Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-07-2013, 07:02 AM   PM User | #1
skeletonchoji
New to the CF scene

 
Join Date: Mar 2013
Location: Wisconsin
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
skeletonchoji is an unknown quantity at this point
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")
}
skeletonchoji is offline   Reply With Quote
Old 03-07-2013, 02:16 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,376
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
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>
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
skeletonchoji (03-07-2013)
Old 03-07-2013, 06:23 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
array, mystery, prompt, sports

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:49 PM.


Advertisement
Log in to turn off these ads.