Code not read properly


#1

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

it prints "your name wasn't found!" although my name is in the text. why? my code is below:

var text= " charles is in the kitchen eating turkey and watching charles angels";
var myName = "charles";
var hits = [];
for (var i=0; i<=text.length; i++){
    if (text[i] === myName[0]){for (var j=i; j<i+myName.length; j++) {hits.push(text[j]);}
    }
    }
    if (text[i] !== myName[0]){
        console.log("Your name wasn't found!");
    }
    else {
        console.log(hits);
        }

#2

your if condition is wrong, you should check if the length of hits is greater then 0


#3

I changed the second if statement to either statements below and the hit was logged. Thanks!

if (hits[0] !== "c"){
console.log("Your name wasn't found!");

Or

if (hits[j] < 0){
console.log("Your name wasn't found!");


#4

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