Search Text for Your Name - Question



Add your if statement in the body of your for loop. It should check to see whether the current letter is equal to the first letter of your name. (Capitalization counts!)

There's no need to put anything between the {}s of your if just yet.

My question: Why do I use text variable instead of myName variable in the if statement? I'm wondering why myName doesn't work.

/*jshint multistr:true */
var text = ('My name is Jiovan and Jiovan likes to code.');
var myName = ('Jiovan') ;
var hits = [];

for (var i = 0; i >= text.length; i++) {
    if (text[i] === 'J') {       
    // check current letter
    // letter = first letter of name



The task at hand is to find instances of your name inside the text string. The if statement is where that check happens. If you were to check whether myName contains the first letter of your name, you would always get a True back and it wouldn't be all that useful.

Another observation: i >= text.length will not allow the for loop to run because the initial state of i is 0, which is not greater than text.length. You need a condition that ends the loop at an appropriate time. i < text.length would allow it to run until the end of text is reached and no longer.


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