Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. // ☆彡  ★彡  ☆彡  ★彡 ☆彡  ★彡  ☆彡  ★彡 ☆彡  ★彡  ☆彡 
  2. // 。.:*・°☆.。.:*・°☆ ADAM'S SUPER COOL HOMEWORK ・°☆.。.:*・°☆.。.:*・°
  3. // ☆彡  ★彡  ☆彡  ★彡 ☆彡  ★彡  ☆彡  ★彡 ☆彡  ★彡  ☆彡  
  4.  
  5.  
  6. /* PROBLEM 1:
  7.  
  8. Write a code with a variable dice that generates a random whole number
  9. between 1 and 6. Then create if statements, so that if, for example, the value
  10. of the dice is 1, then print out "Move 1 space forward." Do the same thing
  11. for all possible values of the dice.
  12. */
  13.  
  14. // Option 1:
  15. var dice = Math.floor((Math.random() * 6) + 1);
  16.  
  17. if(dice == 1){
  18. console.log("Move 1 space forward");
  19. }
  20. else if(dice == 2){
  21. console.log("Move 2 spaces backward");
  22. }
  23. else if(dice == 3){
  24. console.log("Move 3 spaces forward");
  25. }
  26. else if(dice == 4){
  27. console.log("Move 4 spaces backward");
  28. }
  29. else if(dice == 5){
  30. console.log("Move 5 spaces to the right");
  31. }
  32. else if(dice == 6){
  33. console.log("Move 6 spaces to the left");
  34. }
  35.  
  36. // Option 2:
  37. var myDice = Math.ceil(Math.random()*6);
  38.  
  39. switch (myDice) {
  40. case 1 :
  41. console.log('Move 1 space forward.');
  42. break;
  43.  
  44. case 2:
  45. console.log('Move 2 spaces forward.');
  46. break;
  47.  
  48. case 3:
  49. console.log('Move 3 spaces forward.');
  50. break;
  51.  
  52.  
  53. case 4:
  54. console.log ('move 4 spaces forward.');
  55. break;
  56.  
  57. case 5:
  58. console.log ('Move 5 spaces forward.');
  59. break;
  60.  
  61. case 6:
  62. console.log ('Move 6 spaces forward. ');
  63. break;
  64.  
  65. default:
  66. console.log ('NaN');
  67. break;
  68. }
  69.  
  70. /* PROBLEM 2:
  71.  
  72. Write a code that determines whether a number is even or odd. Test your code
  73. by creating a variable called input, and set its value to even or odd numbers.
  74.  
  75. Also write a code that determines whether a number is prime.
  76. */
  77.  
  78. // Input
  79. var input = 4;
  80.  
  81. // Function calls
  82. OddOrEven(input);
  83. IsPrime(input);
  84.  
  85. // Function to tell whether a number is odd or even
  86. function OddOrEven(value){
  87. if((value%2) === 0){
  88. console.log("Even");
  89. }
  90. else if((value%2) === 1){
  91. console.log("Odd");
  92. }
  93.  
  94. return;
  95. }
  96.  
  97. // Function to tell whether a number is prime
  98. function IsPrime(value){
  99. var output = true;
  100.  
  101. for(var i = 2; i < value; i++) {
  102. if(value % i === 0) {
  103. output = false;
  104. }
  105. }
  106.  
  107. if(output){
  108. console.log(value + " is a prime number.");
  109. }
  110. else{
  111. console.log(value + " is not a prime number");
  112. }
  113.  
  114. return;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement