hw6.js 3.4 KB

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