1

I am trying to take a pet name from the user which ends with the letter y if the user enters the name Sammy so it should print that you enter the right name otherwise nothing. But when I enter this name, Sammy

var petName=prompt("Enter your pet name?")
var x=petName.length-1
alert("index number of last latter is: "+x)
if(petName[x]=="y"){
   
   console.log("You have entered the right name.")    
}

first, it showing in the alert dialog the following message

the index number of the last latter is: NoN

second if the statement is not executing.

2
  • .lenght should be .length. Also your closing brace is an opening brace, but that's probably not in the actual code you're running, otherwise you'd not get any alerts. Commented Jul 7, 2020 at 18:47
  • sorry In the editor it was right like length and the closing brace was also correct but still it giving the same output Commented Jul 8, 2020 at 3:07

3 Answers 3

4

You just mistyped length as lenght. Everything else is fine, good job!

Sign up to request clarification or add additional context in comments.

Comments

2

Your almost there. Just have to change the .lenght to .length:

var petName=prompt("Enter your pet name?")
var x=petName.length-1
alert("index number of last letter is: "+x)
if(petName[x]=="y"){
   
   console.log("You have enter the right name.")    
}

Comments

0
var id = "ctl03_Tabs1";
id.substr(id.length - 1);//get the last character
id.substr(2);//get the characters from the 3rd character on
id.substr(2, 1);//get the 3rd character
id.substr(2, 2);//get the 3rd and 4th characters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.