Log it! Console log doesn't give what is expected


#1


Log it!


My code works, but the console gives me this answer:
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
Your name wasn't found!
[ 'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan',
'Kaloyan' ]


var text = " What is going on, Kaloyan?  Ahh you know the same \
shit as usual. Yesterday someone called me by my name Kaloyan, \
which was unusual, but i told myself, what the heck.";
var myName = "Kaloyan";
var hits = [];
for (var i=0; i<=text.length; i++) {
    if (text[i] === "K") {
        for( var j = i; j < i + myName.length; j++) {
            hits.push("Kaloyan");
        }
    }
    else {
        console.log("Your name wasn't found!")
    }
}
if(hits.length == 0) {
    console.log("Your name wasn't found");
}
else {
    console.log(hits);
}


#2

Here

You should make it

if (hits.length === 0) {

Also,

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

Finally, to avoid all that "Your name wasn't found!" printing, you should remove this else block.


#3

This line perfectly demonstrates how .push works :slight_smile:


#4

Yeah, sounds like tautology, it could have been

hits.push(text[j]);

I thought to leave it out.


#5

remove the else part of the first if statement...


#6

Thank you for the advice. I did what you suggested, but the console still logs my name a lot of times.
Here is how my code looks after I made the changes. I think that I do something wrong with hits.length, but that is because I am not sure how itreally works.

var text = " What is going on, Kaloyan? Ahh you know the same \
■■■■ as usual. Yesterday someone called me by my name Kaloyan, \
which was unusual, but i told myself, what the heck.";
var myName = "Kaloyan";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i] === "K") {
for( var j = i; j < i + myName.length; j++) {
hits.push("Kaloyan");
}
}
}
if(hits.length === 0) {
console.log("Your name wasn't found");
}
else {
console.log(hits);
}


#7

Here,

Rather, tell it to

hits.push(text[j]);

#8

Thanks, just to make sure that I understood the difference correctly.
By typing hits.push(text[j]); the computer searches and prints out the name letter by letter, while if I type hits.push("Kaloyan");
it prints out the name for every letter that the name actually has.


#9

Exactly! Spot on! :slight_smile:


#10

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