4/7 Your if statement


#1

Can some one help me with the mistake in the code it is showing me the following 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!"
Thanks in Advance
/*jshint multistr:true */
var text= "rahul karthik balu srikanth naga rahul veeru rahul/"
var myName="rahul"
var hits= []

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

if(myName[i] === "r")
{
}

}


#2

You are only checking if a letter in text is equal to r. If it is the program will do nothing since you put nothing inside the curly brackets after the if statement.


#3

Hi thanks for ur quick response but here is the problem statement

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.


#4

myName is not an array it is a string.


#5

Rahul, maybe it's best to start using semicolons ( ; ) at the end of your statements (in this case the var statements). Might help.
And not for the syntax but for readability: indent your statements when inside another.

You are using text.length in your for loop, but referencing myName inside it instead of text. In fact you'd probably get an error here when executing the code, since text.length is much bigger than myName.length and you would reference a character in myName that doesn't exist.


#6

Strings are arrays of characters and can be referenced in the same way you would reference an array.


#7

Thank you so much. I got it and the problem is resolved.


#8

Hi can you please help me with this code...
It shows ReferenceError: Pradeep is not defined

/*jshint multistr:true */
var text = "Pradeep";"blah";"blah";"blah";"Pradeep";"blah";
var myName = Pradeep;
var hits = [];

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

if (text[i] === 'P'){
}

}


#9

Hi,
first, your text will be no more than "Pradeep" because all the other stuff on that line is in error.
Either create text with one line of text between "" or use "word" + "word2" + "etc.";

The error you get is caused by this: you want Pradeep to be the value of myName. Instead, you are trying to give it the value of Pradeep, which is no declared variable. You should use "Pradeep" instead. Use "";

And another thing: you run the for loop while i<=text.length. As you have seen in the course if the array or string has 6 elements, length will return 6, but the index should run from 0 o 5. So you should run the for loop while i < text.length to avoid errors.


#10

man l also have the same problem can you help


#11

var text = "How are you? My name is Vesela.";
var myName = "Vesela" ;
var hits = [] ;
for ( var i = 0; i < text.length; i++ ) {
if (text[i] === "v") {
}
}


#12

thanks for this explanation, I did not realise we should not forget the " " I thought these were optional lol.


#13

i could be wrong about this but...I think the reason your code is not working is because you are asking it to look for a 'v' in lowercase

whereas in your code you have actually written your name with an Uppercase 'V'

I would try changing your lowercase 'v' to an upper case 'V' and run the code again to see if it works.

Hope that helps - if I am wrong - sorry. Perhaps a fellow learner better skilled in debugging can find the cause of the error for you.