1. random.js
//random number between 1 and 6:
dice = Math.floor((Math.random() * 6) + 1);
alert(dice);
.js
2. float.js
maxTime = 20.00; //20 seconds
minTime = 5.00; //5 seconds
actualTime = Math.random() * (maxTime - minTime) + minTime;
alert(actualTime.toFixed(2));
//^^ .toFixed(2) rounds to decimal places!
.js
3. games.js
//random number between 1 and 3:
computerGuess = Math.floor((Math.random() * 3) + 1);
computerHand = "";
switch(computerGuess){
case 1:
computerHand = "PAPER";
break;
case 2:
computerHand = "ROCK";
break;
case 3:
computerHand = "SCISSORS";
break;
}
alert("Computer chose: " + computerHand);
.js
- Generate a random number between 1 and 52.
- Generate a random decimal number between 0 and 100.
- Can you add a second dice to the dice roll game, and tally the two dice? So that you could use this script in a game of Monopoly.
- Generate a random number between 1 and 3, and this time, spell out the random number (depending on what it is), e.g. "One", "Two", or "Three".
- Can you add input to the Paper Rock Scissors game, so that I can play against (and challenge) the computer?
- Can you make a maths game, that generates a random sum that I have to answer (e.g. 4 + 5, 6 - 3, etc.) When I answer the sum, it should say correct or incorrect. The whole program should be random (including the operation - multiplication, subtraction, addition etc.) To make the program very difficult, see if you can work out how to exclude negative answers, so that if I am asked a subtraction problem, the result isn't a negative number.