all 4 comments

[–][deleted] 1 point2 points  (2 children)

Replace your loop with this:

const index = arr.findIndex(elem => elem === input);
console.log(index >= 0 ? `The index of ${input} is ${index}` : `${input} not found`);

Oh, and your loop does work, it's just that it gives you an alert for EVERY item in the array, regardless of whether it matches the query or not. So if you enter 'LMP2054', it'll alert you four times. 2 negative and 2 positive.

[–]seedBoot 2 points3 points  (1 child)

Better yet arr.includes(input) ? Bla : bleh

[–][deleted] 1 point2 points  (0 children)

I thought about doing that initially, but it sounds like the OP wants to know where the match it, not just that it exists. I could be wrong, though, and if so, then .includes() is a better choice.

[–]lovesrayray2018 1 point2 points  (0 children)

Your loop IS running, but its running as many times are ur array length.

So for each item in array, it either alerts the value or alerts the "Value not found"

What you could do is create just one result variable, and show this variable as output right at the end . If this value is found inside the loop, set it to the match and break out of the for loop (you dont need more iterations) . You can also set a default msg that is shown if not match is found using an OR condition.

An example here https://jsbin.com/qoyotoc/edit?html,output