Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Location
    Wisconsin
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Mystery words prompt = array

    I've been working on this for several hours now and i can't quiet figure out what i'm doing wrong. What i have to do is prompt the user for them to guess what is one of the mystery words, the words are stored in an array. Every time i try to get it to equal it says it doesn't so please i would like some help.
    Code:
    var Guess;
    var arySports = new Array("Football","Basketball","Rollerblading","Hiking","Biking","Swimming");
    
    Guess = prompt("Guess a mystery sport", "")
    if (Guess == arySports){ 
    alert("Congrats");
    }
    else {
    alert("Please pick again")
    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,445
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Two things makes this non working.
    First: You need to randomly pick something from the array. I added this line to do that
    Code:
    var random = arySports[Math.floor(Math.random()*arySports.length)];
    Second: Your words start with a capital. If the players does not capitalize there will be no match so I made everything lower case.
    The code:
    Code:
    <script type="text/javascript">
    var Guess;
    var arySports = new Array("Football","Basketball","Rollerblading","Hiking","Biking","Swimming");
    var random = arySports[Math.floor(Math.random()*arySports.length)];
    
    Guess = prompt("Guess a mystery sport", "")
    if (Guess.toLowerCase() == random.toLowerCase()){
    alert("Congrats");
    }
    else {
    alert("Please pick again")
    }
    </script>

  • Users who have thanked sunfighter for this post:

    skeletonchoji (03-07-2013)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Don't use prompt or alert - they are for debugging only and either include a checkbocx to turn JavaScript off or a checkbox to turn the debugging alerts such as prompt and alert off.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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