main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // a
  2. function a() {
  3. alert('Привет');
  4. }
  5. a()
  6. // cube
  7. function cube() {
  8. console.log(prompt('number') ** 3);
  9. }
  10. cube()
  11. // avg2
  12. function avg2(a, b) {
  13. console.log((a + b) / 2);
  14. }
  15. avg2(5, 10)
  16. avg2(1, 2)
  17. // sum3
  18. function sum3(a, b, c) {
  19. a = a || 0;
  20. b = b || 0;
  21. c = c || 0;
  22. return a + b + c;
  23. }
  24. console.log(sum3(1, 2, 3))
  25. console.log(sum3(5, 10, 100500))
  26. console.log(sum3(5, 10))
  27. // intRandom
  28. // function intRandom(min, max) {
  29. // min = min || 0;
  30. // max = max || 0;
  31. // let result = Math.random() * (max - min) + min;
  32. // return Math.round(result)
  33. // }
  34. function intRandom(min, max) {
  35. if (max === undefined) {
  36. max = min
  37. min = 0
  38. }
  39. return Math.floor(Math.random() * (max - min + 1) + min)
  40. }
  41. console.log(intRandom(2, 15))
  42. console.log(intRandom(-1, -1))
  43. console.log(intRandom(0, 1))
  44. console.log(intRandom(10))
  45. // greetAll
  46. function greetAll(...arguments) {
  47. for (let arg of arguments) {
  48. alert(`Hello ${arg}!`)
  49. }
  50. }
  51. greetAll("Superman")
  52. greetAll("Superman", "SpiderMan")
  53. greetAll("Superman", "SpiderMan", "Captain Obvious")
  54. // sum
  55. function sum(...arguments) {
  56. let result = 0
  57. for (let arg of arguments) {
  58. result += arg
  59. }
  60. console.log(result)
  61. }
  62. sum(1)
  63. sum(2)
  64. sum(10, 20, 40, 100)
  65. // Union
  66. var sample = prompt("Введите название задания")
  67. switch (sample.toLowerCase()) {
  68. case "a": a()
  69. break;
  70. case "cube": cube()
  71. break;
  72. case "avg2": avg2()
  73. break;
  74. case "sum3": sum3()
  75. break;
  76. case "intRandom": intRandom()
  77. break;
  78. case "greetAll": greetAll()
  79. break;
  80. case "sum": sum()
  81. break;
  82. }
  83. // Union declarative
  84. var sample = prompt("Введите название задания").toLowerCase()
  85. var sample2;
  86. var sample1 = {
  87. a: a(),
  88. cube: cube(),
  89. avg2: avg2(),
  90. sum3: sum3(),
  91. intRandom: intRandom(),
  92. greetAll: greetAll(),
  93. sum: sum(),
  94. }
  95. sample2 = sample[`${sample}`]