If else doubt using loop

Write a program to search if a number exists in an array. If the number exists then return the position. If it exists multiple times then return an array with all the positions. If the number doesn’t exist then return -1

1 Like

@pujak816 ,

Try this and let me know here if it works well.

var x=1
var arr4 = [1, 2, 3, 4, 5, 1, 3,1, 1, 4];
var result = [];
for (i = 0; i < arr4.length; i++) {
  if (arr4[i] === x) {
    result.push(i);
  }
}
  if(result==''){
    console.log(-1)}
  else{
   console.log(result); 
  }

1 Like

Thank you! was stuck in last part but now it’s clear

1 Like