5. Incorrect print


#1



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


I'm not sure what's wrong. I tried a couple of the fixes I found in other answers with no luck. I don't get any errors but the output is all wrong and I can't figure out how to fix it. it is printing way too many characters I think to the array, and then at the bottom it just says undefined a bunch. Bugging me...


 /*jshint multistr:true */
var text = "I, Colten, lord of the Sea Monkeys, have returned to save you all from the dreaded Spaghetti monster"

var myName = "Colten";

var hits = [];

for (var i = 0; i < text.length; i ++) {
    if (text[i] === 'c'); {
        for (var j = i; j < (myName.length + i); j ++); {
            hits.push(text[j])
        }
    }
}
 if (hits.length === 0) {
     console.log("Your name wasn't found!");
 } else {
     console.log(hits);
 }


#2

OK, so I swapped the j and i in this line:

for (var j = i; j < (myName.length + i); j ++); {

so it now reads

for (var i = j; j < (myName.length + i); j ++); {

and all I get is [ undefined ]


#3

@strife.tech,
As answer to your Original Post
-1
For Javascript the semi-colon-; is an End-of-Statement indicator....

If the Javascript interpreter encounters

if (text[i] === 'c');

it will not see the following code-block

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

as being part of the IF statement.....
thus
for every text[i]
the code 2nd FOR-loop code-block will be executed.....

-2
As your name starts with a capital-C
you will have to change the IF condition
into

(text[i] === 'C')

#4

Awesome!! Now I understand a bit better the use of the semi-colon. Thank you very much for you input.


#5

@strife.tech,
Here some

References::

http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements
http://stackoverflow.com/questions/25101936/why-does-adding-a-semicolon-sometimes-break-an-if-statement