Log It! - search for your name weird outcome


#1


Log it!


My code prints out

[ 'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma',
'Kuma' ]


/*jshint multistr:true */

var text = "Hey, how are you \
doing? My name is Kuma. the way my name is written is just like it sound Kuma... Kuma Kuma ";

var myName = "Kuma";

var hits = [];

for (var i = 0; i < text.length; i++) {
         if(text[i] === "K") {
             for (var o = i; o < i + myName.length; o++) { 
                 hits.push("Kuma"); }
             }
}

if(hits.length === 0) {
    console.log("Your name wasn´t found") 
} else { console.log(hits) 
}


#2

It should push Kuma into the hits array a total of 16 times. Your myName string is 4 characters long and your text string contains 4 instances of "Kuma".

This is the expected outcome!

When the for loop encounters the "K" in the text variable it will push that letter and all the letters that follow it to an array, stopping when the number of letters it pushes are equal to the number of letters in your name.


#3

I actually changed the line

hits.push("Kuma"); }

to

hits.push(text[o]); }

#4

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