Code went through fine but I just see [ ] when I run it


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661

There are no error messages and the Codecademy program greenlights me to move on, but my resulting program just shows: [ ] with no letters inside! Thanks in advance.

Here is my code:

/*jshint multistr:true */

var text = "Hey, how are you \ doing? My name is Reid. \ I like the name 'Reid'.";

var myName = "Reid";

var hits = []

for (var i = 0; i < text.length; i++) {
    if (text[i] === 'R') {
        for (var 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);
}

#2

Note that = is the assignment operator, the result of this expression:

hits.length = 0

is 0 which is falsy and will therefore never cause the statement for that if-statement to execute.

It also has another side effect, which is setting the length to 0.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.