script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // ________________________________________________FUNCTIONS__________________________________________________________
  2. // --------------------------------------------a--------------------------------------------------------------
  3. function a(key) {
  4. alert(key);
  5. }
  6. // a("Hello Stranger");
  7. // --------------------------------------------cube--------------------------------------------------------------
  8. function cube(number) {
  9. return number = number * number * number;
  10. }
  11. // alert(cube(3));
  12. // --------------------------------------------avg2--------------------------------------------------------------
  13. // let digit1 = +prompt("Enter first digit");
  14. // let digit2 = +prompt("Enter second digit");
  15. function avg2(num1, num2) {
  16. let avg = (num1 + num2) / 2;
  17. return avg;
  18. }
  19. // alert(avg2+prompt("Enter first digit"), +prompt("Enter second digit")));
  20. // --------------------------------------------sum3--------------------------------------------------------------
  21. // let digit1 = +prompt("Enter first digit");
  22. // let digit2 = +prompt("Enter second digit");
  23. // let digit3 = +prompt("Enter third digit");
  24. function sum3(num1, num2, num3) {
  25. let sum = num1 + num2 + num3;
  26. return sum;
  27. }
  28. // alert(sum3(digit1, digit2, digit3));
  29. // let sum3 = (num1, num2, num3) => {
  30. // let sum;
  31. // return sum = num1 + num2 + num3; // Just wanted to try Arrow Functions
  32. // }
  33. // alert(sum3(+prompt("Enter first digit"), +prompt("Enter second digit"), +prompt("Enter third digit")));
  34. // --------------------------------------------intRandom--------------------------------------------------------------
  35. // function intRandom(min, max) {
  36. // let num = Math.round(Math.random() * max); // Need to fix
  37. // if(num < 0) {
  38. // num -= min;
  39. // }
  40. // if(num < min) {
  41. // num = num + min;
  42. // }
  43. // return num;
  44. // }
  45. function intRandom(min, max) {
  46. if(max != prompt) {
  47. max = min;
  48. min = 0;
  49. }
  50. let num = min - 0.5 + Math.random() * (max - min + 1);
  51. return Math.round(num);
  52. }
  53. // alert(intRandom(+prompt("Enter Min value"), +prompt("Enter Max value")));
  54. // console.log(intRandom());
  55. // --------------------------------------------greetAll--------------------------------------------------------------
  56. // function greetingsAll(name) { // It was my very first try
  57. // alert(`Hello ${name}`);
  58. // }
  59. // greetingsAll(prompt("Enter the name to say hi to this person"));
  60. //_____________________________________________This one is done as in requirements___________________________________________________________________
  61. function greetingsAll() {
  62. let names = [];
  63. for(let param of arguments) {
  64. names.push(param);
  65. }
  66. alert(`Hello ${names}`);
  67. }
  68. // greetingsAll("Sam", "Dean", "Jonathan");
  69. // --------------------------------------------sum--------------------------------------------------------------
  70. function sum() {
  71. let total = 0;
  72. for(let key of arguments) {
  73. total += key;
  74. }
  75. return total;
  76. }
  77. // alert(sum(10, 25, 33, 46, 56));
  78. // --------------------------------------------Union--------------------------------------------------------------
  79. // let union = prompt("Введите название задания")
  80. // switch (union.toLowerCase()){
  81. // case "a": a("Hello Stranger");
  82. // break
  83. // case "cube": alert(cube(3));
  84. // break
  85. // case "avg2": alert(avg2(+prompt("Enter first digit"), +prompt("Enter second digit")));
  86. // break
  87. // case "sum3": alert(sum3(+prompt("Enter first digit"), +prompt("Enter second digit"), +prompt("Enter third digit")));
  88. // break
  89. // case "intrandom": alert(intRandom(+prompt("Enter Min value"), +prompt("Enter Max value")));
  90. // break
  91. // case "greetAll": greetingsAll("Sam", "Dean", "Jonathan");
  92. // break
  93. // case "sum": alert(sum(10, 25, 33, 46, 56));
  94. // break
  95. // }
  96. // --------------------------------------------Union declarative--------------------------------------------------------------
  97. let taskNames = {
  98. "a": a,
  99. "cube": cube,
  100. "avg2": avg2, // Doesn't work for now
  101. "sum3": sum3,
  102. "intrandom": intRandom,
  103. "greetAll": greetingsAll,
  104. "sum": sum
  105. }
  106. let taskToStart = prompt("Введите название задания");
  107. for(let key in taskNames) {
  108. if(key === taskToStart) {
  109. alert(key);
  110. }
  111. }