6 -Did I code wrong?


#1

EDIT: Nvm a few next later they say its isn't perfect and what I encountered is a possibility!

My name is harel - 5 letters.
in this exercise we push name.length to the array when it hits the first letter of the name.

when i coded i used "Hi my name is harel and im 21. as I said my name is harel so yeah.. thats that!"
when i wanted to see what is in my array i noticed that after every "h" the code send 5 letters to the array but not only the letters from my name. in my text I used "thats that!" and the h,a,t in "that" got sent to the array too.
did i code wrong or just a coincidence and the developers haven't thought about this possibly?

var text = "Hi my name is harel and im 21. as I said my name is harel so yeah.. thats that!";
var myName = "harel";
var hits = [];

for (var i=0 ; i < text.length ; i++) {
    if (text[i] === "h") {
    for (var j = i ; j < (myName.length + i) ; j++) {
        hits.push(text[j]);
    
    }
}
    
}
console.log(hits);

#2

@enterylvl,
Well you discovered the flaw of the logic....
How would you change the flow so that only your name is in the hits Array.


#3

Probably use a function like in the rock paper scissor exercise where if the first letter is h but the next letter is not a then it will continue to search.

your thoughts?

well a new question after i finished with the rest of the exercise - the array became blank. then i remove this block of code it returns. why?

  if(hits.length =0) {
        console.log ("your name wasn't found!");
    }
    else    {
      console.log(hits);
    }

#4

@enterylvl,

In an IF condition you will have to
use a comparison operator
instead of you using an assignment operator....

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators