Help w my code, prints out wrong response


#1

it says well done, but it prints out "your name wasnt found", although my name is in text.

var text = "My name is Luca mao Luca"

var myName = "Luca"

var hits = []

for(i = 0; i < text.length; i++){

    if(text[i] === "L"){
        
        for(j = i; j <= (i + myName.length);j++){
            
            hits.push(text[j])
            
        }
    }
}
if(text[i] !== "Luca"){
    console.log("Your name was not found")
}
else{
    console.log(hits)
}


#2

Your if condition is doing exactly what it is written to do. Try changing the condition. What do you want the condition to check for? Since the else statement is printing out the value of hits, maybe have the if condition check if hits has nothing assigned to it.


#3

so if hits is empty?


#4

if i do that it just gives me a syntax error: ].
i dont think i understand how to write an array as having nothing in it


#5

Try hits===0 in your if condition:

if (hits===0)


#6

Thnx this worked . ............... . ............................


#7

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