dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Testing user input against an array and displaying result

  1. #1
    Join Date
    Sep 2012
    Location
    Philadelphia, Pennsylvania
    Posts
    2

    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>

    </body>

  2. #2
    Join Date
    Sep 2012
    Location
    Philadelphia, Pennsylvania
    Posts
    2
    Thanks anyway, but I got a quicker reply on Yahoo answers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles