script.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // a
  2. function a(msg) {
  3. return alert(msg)
  4. }
  5. // cube
  6. function cube(num) {
  7. return num**3
  8. }
  9. //avg2
  10. function avg2(num1, num2) {
  11. return (num1 + num2) / 2
  12. }
  13. //sum3
  14. function sum3(num1, num2, num3=0) {
  15. return num1 + num2 + num3
  16. }
  17. //intRandom
  18. function intRandom(min, max) {
  19. let temp = min
  20. min = isNaN(max)? 0 : Math.ceil(min)
  21. max = Math.floor(max) || temp
  22. res = (Math.random() * (max - min) + min) <= 0? Math.ceil(Math.random() * (max - min) + min) : Math.floor(Math.random() * (max+1 - min) + min)
  23. return res === -0? 0 : res
  24. }
  25. //greetAll
  26. function greetAll() {
  27. let greet = 'Hello '
  28. for(let i = 0; i < arguments.length; i++) {
  29. greet += i === arguments.length - 1? `${arguments[i]}` : `${arguments[i]}, `
  30. }
  31. return alert(greet)
  32. }
  33. //sum
  34. function sum() {
  35. let sum = 0
  36. for(let i = 0; i < arguments.length; i++) {
  37. sum += arguments[i]
  38. }
  39. return sum
  40. }
  41. //Union
  42. function aSample() {
  43. a('HELLO!')
  44. }
  45. function cubeSample() {
  46. return cube(5)
  47. }
  48. function avg2Sample() {
  49. return avg2(10, 5)
  50. }
  51. function sum3Sample() {
  52. return sum3(5,10,100500)
  53. }
  54. function intRandomSample() {
  55. return intRandom(15, 27)
  56. }
  57. function greetAllSample() {
  58. greetAll('Mihail', 'Bogdan', 'Denis')
  59. }
  60. function sumSample() {
  61. return sum(2, 3, 4, 5, 10)
  62. }
  63. let sample = prompt('Введите название задания','') || ''
  64. switch (sample.toLowerCase()){
  65. case "a":
  66. aSample()
  67. break;
  68. case "cube":
  69. console.log('cube: ',cubeSample())
  70. break;
  71. case "avg2":
  72. console.log('avg2: ',avg2Sample())
  73. break;
  74. case "sum3":
  75. console.log('sum3: ',sum3Sample())
  76. break;
  77. case "intrandom":
  78. console.log('intRandom: ',intRandomSample())
  79. break;
  80. case "greetall":
  81. greetAllSample()
  82. break;
  83. case "sum":
  84. console.log('sum: ',sumSample())
  85. break;
  86. default:
  87. alert('совпадений не найдено.')
  88. break;
  89. }
  90. let functionSamples = {
  91. a: aSample,
  92. cube: cubeSample,
  93. avg2: avg2Sample,
  94. sum3: sum3Sample,
  95. intrandom: intRandomSample,
  96. greetall: greetAll,
  97. sum: sumSample
  98. }
  99. alert('STARTING FROM OBJECT')
  100. console.log('started from sample object:', functionSamples[sample]())