hw6.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // a ==============================================================================
  2. function a(someStr){
  3. alert(someStr)
  4. }
  5. // a("Привет")
  6. // a("Как дела?")
  7. // a("Как погода?")
  8. // cube ==============================================================================
  9. function cube(ourNum){
  10. let result
  11. result = ourNum**=3
  12. console.log(result)
  13. }
  14. // cube(17)
  15. // avg2 ==============================================================================
  16. function avg2(firstNum,secondNum){
  17. let result
  18. result = (firstNum + secondNum)/2
  19. console.log(result)
  20. }
  21. // avg2(7,10)
  22. // sum3 ==============================================================================
  23. function sum3(firstNum,secondNum,thirdNum){
  24. let result
  25. if(thirdNum===undefined){
  26. thirdNum=0
  27. }
  28. else if(secondNum===undefined){
  29. secondNum=0
  30. }
  31. else if (thirdNum===undefined){
  32. thirdNum=0
  33. }
  34. result = (firstNum + secondNum + thirdNum)
  35. console.log(result)
  36. }
  37. // sum3(1,2,3)
  38. // intRandom ==============================================================================
  39. function intRandom (minNum, maxNum){
  40. let result
  41. if(maxNum===undefined){
  42. maxNum=minNum
  43. minNum=0
  44. }
  45. result = Math.round(Math.random() * (maxNum - minNum ) + minNum)
  46. console.log(result)
  47. }
  48. // intRandom(2,15)
  49. // intRandom(-1,-1)
  50. // intRandom(0,1)
  51. // intRandom(10)
  52. // greetAll ==============================================================================
  53. function greetAll() {
  54. let result = ''
  55. for(let i=0; i<arguments.length; i++){
  56. result+=arguments[i]+', '
  57. }
  58. result = result.slice(0,-2)
  59. alert('Hello '+result)
  60. }
  61. // greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
  62. // sum ==============================================================================
  63. function sum(){
  64. let result = 0
  65. for(i=0; i<arguments.length; i++){
  66. result +=arguments[i]
  67. }
  68. console.log(result)
  69. }
  70. // sum(1)
  71. // sum(2)
  72. // sum(10,20,40,100)
  73. // Union ==============================================================================
  74. function aChoice(){
  75. a("Привет")
  76. a("Как дела?")
  77. a("Как погода?")
  78. }
  79. function cubeChoice(){
  80. cube(17)
  81. }
  82. function avg2Choice(){
  83. avg2(7,10)
  84. }
  85. function sum3Choice(){
  86. sum3(1,2,3)
  87. }
  88. function intRandomChoice (){
  89. intRandom(2,15)
  90. intRandom(-1,-1)
  91. intRandom(0,1)
  92. intRandom(10)
  93. }
  94. function greetAllchoice(){
  95. greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
  96. }
  97. function sumChoice(){
  98. sum(1)
  99. sum(2)
  100. sum(10,20,40,100)
  101. }
  102. let taskChoice = prompt('Введите название задания')
  103. switch(taskChoice.toLowerCase()) {
  104. case 'a': aChoice()
  105. break;
  106. case 'cube': cubeChoice()
  107. break;
  108. case 'avg2': avg2Choice()
  109. break;
  110. case 'sum3': sum3Choice()
  111. break;
  112. case 'intrandom': intRandomChoice()
  113. break;
  114. case 'greetall': greetAllchoice()
  115. break;
  116. case 'sum': sumChoice()
  117. break;
  118. }
  119. // Union declarative ==============================================================================
  120. let taskChoiceTwo = prompt('Введите название задания')
  121. const functionObj = {
  122. 'a': aChoice,
  123. 'cube': cubeChoice,
  124. 'avg2': avg2Choice,
  125. 'sum3': sum3Choice,
  126. 'intrandom': intRandomChoice,
  127. 'greetall': greetAllchoice,
  128. 'sum': sumChoice
  129. }
  130. functionObj[taskChoiceTwo.toLowerCase()]()
  131. // ============================================================================================================================================================