Codecademy Javascript error

Asked

Viewed 162 times

1

I’m having trouble creating a for inside the if and taking the value of i and inside the second is with var j. I need to compare the var myName to the text and check if they start with the same letter and then do var hits with the command. push() with variables i and j to present.inserir a descrição da imagem aqui

  • 3

    Posting the code is better than the image.

1 answer

2

Hi, I think I’d look like this:

var text = "my name is Emerson";
var myName = "Emerson";
var hits = [];
for(i=0; i< text.length; i++){
   if(text[i] === myName[0]){
      for (var j = i; j < myName.length+i; j++) {
        hits.push(myName[j-i]);
      };
   }
}

Hugging, good study.

  • Oops, try again. Caution: Your second lac 'for' should be stopped when it reaches the current point in the string + myName.length. I PUT YOUR CODE BUT IT SHOWS THIS MESSAGE ABOVE.

  • What exactly does he ask in the question ? In the picture it was cut and in your question it was not so clear...

  • Ask for it, Your second "for" loop Ok! Last step: add another for loop, this time within the body of your if statement (enter the {}s of if). This loop will ensure that each character of your name is pushed (push) into the final array. The if statement says, "If we find the first letter of the name, start the second loop for!" This loop says: "I will add characters to the array until it reaches the length of the user name". So, if your name has 11 letters, your loop should add 11 characters to hits if it finds the first letter of myName in text. For your second loop is, have the

  • the rest. it finds the first letter of myName in text. First, you must make the second loop iterator start at the first loop, so it starts where the first loop stopped. If your first loop starts at is(var i = 0; // rest of the loop setting your second should look like for(var j = i; // rest of the loop setting Second, think about how your loop should be stopped. Read the Hint if you need help! Finally, in the body of your loop, make the program use the . push() method of hits. Just as strings and arrays have a method . length, arrays have

  • arrays have an . push() method that adds whatever is in parentheses to the end of the array. For example, newArray = []; newArray.push('Alo'); newArray[0]; // and equal to 'Alo' Instructions OK! Go ahead and add the second loop inside the body of your if statement. ? Tip Your loop must be stopped when it reaches the value of the first iterator

  • All right, look now.

  • Agr worked, man, thanks for the attention.

  • If the answer was helpful to you, ask it to help other community members, abs, good studies.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.