Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4
Thread: Message Duplicate

06092014, 12:32 AM #1
 Join Date
 Jun 2014
 Posts
 4
 Thanks
 1
 Thanked 0 Times in 0 Posts
Message Duplicate
Code:<html> <head> </head> <body> <script type="text/javascript"> var randomNumber1 = Math.round(Math.random() * 1) var randomNumber2 = Math.round(Math.random() * 1) var chances = 5 function Random(){ if(Numbers.Guess1.value != randomNumber1 && Numbers.Guess2.value != randomNumber2 ) { document.write("Numbers generated are: " + randomNumber1 + " and " + randomNumber2) document.write("<br>You win!") } else(Numbers.Guess1.value == randomNumber1 && Numbers.Guess2.value == randomNumber2 ) { document.write("Numbers generated are: " + randomNumber1 + " and " + randomNumber2) document.write("<br>You lose!") } } </script> <form name=Numbers> <input type="text" name=Guess1>Enter your Number 1!<br> <input type="text" name=Guess2>Enter your Number 2!<br> <input type=button value="Confirm Number" onClick="Random()"> </form> </body> </html>
Should be like this:
Enter number: 10
Enter number: 15
Number generated as 3 and 3
You have lost
You have 2 chance(s) left to play the game

Enter number: 2
Enter number: 0
Number generated as 0 and 3
You have lost
You have 1 chance(s) left to play the game

Enter number: 2
Enter number: 2
Number generated as 1 and 3
You have lost
Unfortunately there are no more chances, have a nice day

Enter number: 2
Enter number: 3
Number generated as 2 and 3
Congratulations you have won
Have a fantastic day!


06092014, 01:08 AM #2
 Join Date
 Jun 2014
 Posts
 4
 Thanks
 1
 Thanked 0 Times in 0 Posts
Error i get in Google Chrome
Numbers generated are: 0 and 0
You win!Numbers generated are: 0 and 0
You lose!

06092014, 01:57 AM #3
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 9,213
 Thanks
 4
 Thanked 925 Times in 912 Posts
Math.random()  produces a random number where 0 <= number < 1
multiplying it by 1 does nothing.
Math.round()  will round the number to the nearest integer giving you either 0 or 1 as the result.
This means that there are four possible combinations 0 0, 0 1, 1 0, and 1 1 with zero being slightly more likely than 1 in each case.
To get what you want with the random numbers you need to replace the Math.round with Math.floor and replace the 1 with one greater than the highest number you want the random number to be (assuming you want 0 to be the lowest number) otherwise you need to add the minimum number to the result and replace 1 by the difference between the minimum and maximum.
I'd also suggest replacing the obsolete document.write statements with innerHTML so that the form doesn't get overwritten the first time the script runs.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.

Users who have thanked felgall for this post:
Lifepuzzler (06092014)

06092014, 02:06 AM #4
 Join Date
 Jun 2014
 Posts
 4
 Thanks
 1
 Thanked 0 Times in 0 Posts
The 1 was just for testing purpose.
Thank you for your time in responding to my post. Will edit and see what results pop up.