Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡
- // 。.:*・°☆.。.:*・°☆ ADAM'S SUPER COOL HOMEWORK ・°☆.。.:*・°☆.。.:*・°
- // ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡 ★彡 ☆彡
- /* PROBLEM 1:
- Write a code with a variable dice that generates a random whole number
- between 1 and 6. Then create if statements, so that if, for example, the value
- of the dice is 1, then print out "Move 1 space forward." Do the same thing
- for all possible values of the dice.
- */
- // Option 1:
- var dice = Math.floor((Math.random() * 6) + 1);
- if(dice == 1){
- console.log("Move 1 space forward");
- }
- else if(dice == 2){
- console.log("Move 2 spaces backward");
- }
- else if(dice == 3){
- console.log("Move 3 spaces forward");
- }
- else if(dice == 4){
- console.log("Move 4 spaces backward");
- }
- else if(dice == 5){
- console.log("Move 5 spaces to the right");
- }
- else if(dice == 6){
- console.log("Move 6 spaces to the left");
- }
- // Option 2:
- var myDice = Math.ceil(Math.random()*6);
- switch (myDice) {
- case 1 :
- console.log('Move 1 space forward.');
- break;
- case 2:
- console.log('Move 2 spaces forward.');
- break;
- case 3:
- console.log('Move 3 spaces forward.');
- break;
- case 4:
- console.log ('move 4 spaces forward.');
- break;
- case 5:
- console.log ('Move 5 spaces forward.');
- break;
- case 6:
- console.log ('Move 6 spaces forward. ');
- break;
- default:
- console.log ('NaN');
- break;
- }
- /* PROBLEM 2:
- Write a code that determines whether a number is even or odd. Test your code
- by creating a variable called input, and set its value to even or odd numbers.
- Also write a code that determines whether a number is prime.
- */
- // Input
- var input = 4;
- // Function calls
- OddOrEven(input);
- IsPrime(input);
- // Function to tell whether a number is odd or even
- function OddOrEven(value){
- if((value%2) === 0){
- console.log("Even");
- }
- else if((value%2) === 1){
- console.log("Odd");
- }
- return;
- }
- // Function to tell whether a number is prime
- function IsPrime(value){
- var output = true;
- for(var i = 2; i < value; i++) {
- if(value % i === 0) {
- output = false;
- }
- }
- if(output){
- console.log(value + " is a prime number.");
- }
- else{
- console.log(value + " is not a prime number");
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement