6/7 Why is it saying it's right if it prints out something else?! What's wrong?


#1

This is the code:
/*jshint multistr:true */
var text = "leeds leeds leeds united \ leeds leeds leeds \ Nils leeds leeds Nils Nils"

var myName = "Nils"

var hits = []

for (i = 0; i < text.length; i++) {
if (text[i] === "N"){
for (j = i; j < i + myName.length; j++) {
hits.push(text[j])
}
}
}

if(hits.length = 0) {
console.log("Your name wasn't found.");
} else {
console.log(hits);
}

But it prints out to: [ ]

That isn't right. Shouldn't it print my name, somehow? Why does it say it is correct and let me continue?

This exercise really caused me problems.


#2

Wow, I love this post. This bug is so nice!

Firstly, I would like to note that it's really hard to predict every stupidity we (users) can write. Believe me, there are too many possibilities.

The problem is in your if ... else construction:

if (hits.length = 0) {
    console.log("Your name wasn't found.");
} else {
    console.log(hits);
}

hits.length = 0 is an assignation operation, you should check here if length of hits is equal to 0 => you have to use equality check operator === instead of =.

Let me explain what happened. Your hits array is filled with characters of your name and now we are at if. The interpreter takes a look at condition of your if and tries to evaluate it to true or false value. The condition is an assignation operation. So it executes this operation => you set length of the array to 0, it erased all data :smile: and because right side of the operation is a falsy value (0) whole expression evaluates to false. And that is why your script goes to the else block and prints out to the console empty array ([]), because it lost all data inside in the moment when you set length to 0.


#3

Alright, got it. Thanks! I understand it must be hard to cater for all our bollocks :wink: i was just happy that i could preas on, but then felt i actually needed to know what's up.
Thanks for the explanation!


#4

You're very welcome :smile:


#5

thanks it was diffucult be worth your help :sunglasses:


#6

Thanks for the explanation! I had exact question too. However, after fixing it according to your suggestion my array prints out "[ ' . ' , ' ' ]" x2. Shouldn't it print my name?

Here is my code:

var text = "Typing text, I'm typing text. Bryce is typing text, Bryce.";

var myName = "Bryce";

var hits = []

for(var i=0; i < text.length; i++) {
if (text[i] === "B") {
for(var j=i; j < myName.length + i; j++); {
hits.push(text[j]);
}
}
}
console.log(hits);

if(hits.length === 0) {
console.log("Your name wasn't found.");
} else {
console.log(hits);
}

Thanks for any help.


#7

The problem is in your second for loop:

for (var j = i; j < myName.length + i; j++); {
    hits.push(text[j]);
}

Unwanted semicolon after for loop terminates loop. You have to delete it:

for (var j = i; j < myName.length + i; j++) {
    hits.push(text[j]);
}

If you don't know when you should use semicolons read this article -> Your Guide to Semicolons in JavaScript.