Console logs first letter of myName


#1



Completed the Course


So, I technically finished the course. However, my code doesn't actually work. I have looked through the forums and while now and can't seem to find anyone with this issue.
Issue: An array is logged to the console. But, the array only has logged three "M"s and not my whole name.
I've looked over the (correctly written) code on the Q&A for this topic and my code looks identical as far as I can see.
Please help. I really want to understand my code, why it works and why it doesn't work, not just pass the course.


/*jshint multistr:true */
var text = "Hi how are you! My name is Miranda\
A cool story is how I got my name. Since Miranda is pronouced the same in\
Spanish as it in in English my mother named me that so my\
Great grandmother could comfortably pronouce my name.";

var myName = "Miranda"
var hits = []

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

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


#2

Hi this line remove the ; at the end

for (var j = i; i > (myName.length + i); i++); <--

you could read more about that here..

Conditions, Blocks and Semicolons

If you want to understand how it work you could search on the forum there are many topic that explain how it work like these...

Search Text for Your Name further explanation needed

New j variable in challenge 6. “search text for my name”

Hope that could help you


#3

I removed the semicolon and now the console logs that my name was not found at all.
Thank you for the links! I'll review them as soon as I am done with my lesson :smile:


#4

Also here

for (var j = i; i > (myName.length + i); i++)

instead of i > (myName.length + i) change it to j < (myName.length + i)


#5

That was it! Lol I had actually just change it after looking over one of the links you sent me (My lesson can wait!) when my browser gave me the notification. Thank you for your time and help :smiley:


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

Here is the correct block of code for anyone having the same issue :3


#6

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