I'm doing some exercises and having a hard time figuring out why my code won't work, would be glad if someone can point me in the right direction. So my input is a string of numbers, which I .split to get an array of strings (so I can loop trough it). Then I want to compare if the strings are equal to the next one or not, and use it to do something (here I'm just printing out the status). Can't figure out what I did wrong, code below.
var line = "40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86";
line = line.split(" ");
for (x = 0; x < line.lenght; x++) {
if (line[x] == line[x + 1]) {
console.log("numbers are the same");
} else if (line[x] !== line[x + 1]) {
console.log("numbers aren't the same");
}
}
else if()is useless, just go for anelse()for (x = 0; x < line.length - 1; x++)( typo and dereference beyond upper array bound). Consider using the type-safe equality test (===instead of==).