Logical error in my code!


#1


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


Hello, my code runs, and results in "Way to go! / Start Next Lesson", but logically is incorrect. The program cannot find my name "Geri". Could ou please help?


/*jshint multistr:true */
var text = "Hello, this is not a long line, but my name is Geri."
var myName = "Geri"
var hits = []
for (var i = 0; i < text.length; i++);
    if (text[i] === "G");
        for (var j = i; j < myName.length + i ; j++);
            {hits.push('Geri')};
if (hits !== "Geri")
    console.log("Your name wasn't found!")
else
    console.log(hits)


#2
if (text[i] === "G");
    for (var j = i; j < myName.length + i ; j++);
        {hits.push('Geri')};

this means that whenever you find a "G" in the text you add "Geri".length (4) times "Geri" to your hits array. And hits array is actually the keyword for the other problem:

if (hits !== "Geri")

this will probably never be true as hits is an array and not a string.

PS: And probably the mistake you're actually looking for:

    for (var i = 0; i < text.length; i++);
    for (var j = i; j < myName.length + i ; j++);
    if (text[i] === "G");

do not use semicolons after loops or conditionals. Here is a link on what the problem with semicolons after conditional is:


For loops it is quite similar and if you have questions about it feel free to ask.


#3

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