4, Your "if" statement

Hi guys I was wondering if you could help me!

This is my code:

var text= “hey Daniel, how are you doing daniel and why you are so tall Daniel?”
var myName= “Daniel”
var hits = ;

for (var i = 0; i < text.length; i++) {
if (myName[i] === “D”) {
console.log (“Daniel”)
}
}

I’m getting this error:
Oops, try again. Remember how to check the value of an element in an array? You simply do arrayName[i]. Check the Hint if you need help!

You should change this part if (myName[i] === "D") to if (text[i] === "D").
Otherwise you keep searching for D inside your name, and it only contains your name once. To do a proper search, you have to check for D inside text variable that contains your name twice.

1 Like

Hi! can you please help me…

/*jshint multistr:true */
var text=‘Hey, how are you
doing? My name is Elena.’;
var myName=‘Elena’;
var hits = ;
for (var i = 0; i < text.length; i++){
if text[i]===‘E’{
}
}

I get this “Oops, try again. There was a problem with your syntax.”

Hi try to put your if statement like that if (text[i]==='E')

If you’re on the same lesson, this is how your code

var text='Hey, how are you doing? My name is Elena.';
var myName = 'Elena';
var hits = [];
for (var i = 0; i < text.length; i++) {
  if(text[i]==='E') {
    console.log(myName);
  }
}

You missed () around your if loop condition. Otherwise you had everything fine. I hope you understand how the code works too?

wizmarco, igorcaletacar, thank you guys! it worked out!
Actually, I had the brackets in the beginning, but it looks like at a certain point I deleted them, thinking it was a problem with my syntax…

Hello,

I get the right answer here but I’m not sure to understand why “if (text[i] === ‘C’)”, particularly the meaning of “text[i]”.
I can keep going, hoping I’ll finally understand but if it’s so clear for you guys you want to share, I would be really happy to understand the deep meaning of those.

Thanks =)

Hi here

1

for (var i = 0; i < text.length; i++)

This for loops count all the letter inside your text.
If you add a console.log(i) it will print the number of all the letter and also all the space.

2 if statement

if(text[i]==='E') {
console.log(myName) 
}

So this part work like that:
if you see a letter inside text that equale to "E" print myName

Hope I could help you

Thank you very much for that. It’s kinda clearer, even if I don’t get perfectly how we were supposed to know than "text[i] means “put the i inside the text” (if I have properly understood).
If it is the case, I accept it as a fact but I’m not sure to understand perfectly how to use this new informations.
ANYWAY thanks again =)

/*jshint multistr:true */

var text = “Darine, Darine, Darine, Darine”;
var myName = “Darine”
var hits = ;
/I made the variables here/

/* This is the part I’m stuck on */
for(i = 0; i < text.length; i++){
if(text[i]===‘D’) {
console.log(myName)
}

Please help

Hi what’s arror do you get? And in which lesson are you.?

oh sorry I forgot to put that. I am on lesson 4 your "if " statement. The error I get is
Oops, try again. There was a problem with your syntax.

here is a picture

Hi you forgot to close your for loop add one } after your closing if statement

1 Like

thx that really helped. :grinning: