Type something to search...
Solution to Day 5 of 7 Days of JS

Solution to Day 5 of 7 Days of JS

  • JavaScript
  • Challenge
  • Lautaro Lobo
  • 04 Dec, 2019

As before, I first made it with a while loop:

function positionOf(n,array)\{
  let i = 0;
  let count = array.length;
  while (i < count)\{
    if (array[i] == n)\{
      return i;
    } else \{
      i++;
    }
  }
  return -1;
}

console.log(positionOf(5,[-5,0,50,5,35]))

And then, a for loop:

function positionOf1(n,array)\{
  let count = array.length;
  for (let i = 0; i < count; i++)\{
    if (array[i] == n)\{
      return i;
    }
  }
  return -1;
}

console.log(positionOf1(3,[-3,0,3,5,35]))

Do you have a better solution? Share it in the comments!

See you on day 6!


Related Posts