How to print whole list in for loop?

the condition checking if(!(li[i]in x)){ is not correct. in check if keys(index) exist in the array .

Change to if(!x.includes(li[i])){


From Javascript if in x I got

JavaScript does have an in operator, but it tests for keys and not values.

So you don't get those values because you check for the key -> 0, 1, 2, 3, etc. and at the position of e.g. item 1 in your list the 1st key is already used.

In other words what the if in your specific case actually does is if(x.length < li[i]).

Just to clarify if(x.length < li[i]) is not the same as !(li[i]in x) in every case!

To print the whole list use includes for your if. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Tags:

Javascript