Testing user input against an array and displaying result
Hello fellow programmers. This is my first time posting to any board anywhere so please bare with me for a moment. I am new to JavaScript, and have only been studying it for two months now. I have successfully gotten my code to display the results of the array, but now I need my code to take the users input, test it to see if the letter they put is in the array, and then repeat the letter back to the user along with its position in the array.
Here is my code:
<head>
<script type="text/javascript">
var superHero = prompt("Please enter the name of a SuperHero!","Spiderman");
superHero.length = (superHero)+1;
var charArray = new Array();
charArray = superHero.slice(" ");
document.write("There are " + superHero.length + " letters in the word " + superHero) + "<br/>";
for (i = 0; i < charArray.length; i++) {
document.write("<br/><br/>" + "Letter " + charArray[i] + " is number " + (i + 1) + " in the word " + superHero + "<br/>");
}
function whatChar(){
var thisChar = document.getElementById("pickChar").value;
if(thisChar == ""){
alert("Please enter a letter from your superheroes name!")
}else if(thisChar == "?"){ //This is what I can't figure out!
return thisChar;
}else{
alert(thisChar + " is not a letter in your superheroes name!");
pickChar.value = "";
return whatChar();
}
}
</script>
</head>
<body>
<br/>
<label>Please enter a letter from your superheroes name!</label>
<p><input type="text" id="pickChar" name="pickChar"></p>
<button onclick="whatChar()">Please click here to find your letter in your superheroes name!</button>
Bookmarks