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.
.lenghtshould 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.