main.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. function a(text) {
  2. alert(text)
  3. }
  4. const cube = num => Math.pow(num, 3)
  5. const avg2 = (a, b) => (a + b) / 2
  6. const sum3 = (...params) => params.reduce((a, b) => a + b)
  7. const intRandom = (a, b) => !b ? Math.round(Math.random() * a) : Math.round(Math.random() * (b - a)) + a
  8. function greetAll() {
  9. let str = []
  10. for (let i = 0; i < arguments.length; i++) {
  11. str.push(arguments[i])
  12. }
  13. alert('Hello ' + str.join(', '))
  14. }
  15. function sum() {
  16. let arrNum = []
  17. for (let i = 0; i < arguments.length; i++) {
  18. arrNum.push(arguments[i])
  19. }
  20. alert(arrNum.reduce((a, b) => a + b))
  21. }
  22. var sample = prompt("Введите название задания")
  23. switch (sample.toLowerCase()) {
  24. case "a": a('Привет')
  25. break
  26. case "cube": cube(5)
  27. break
  28. case "avg2": avg2(10, 8)
  29. break
  30. case "intRandom": intRandom(5, 25)
  31. break
  32. case "greetAll": greetAll("Superman", "SpiderMan", "Captain Obvious")
  33. break
  34. case "sum": sum(10, 20, 40, 100)
  35. break
  36. }
  37. const objFunction = {
  38. a: a,
  39. cube: cube,
  40. avg2: avg2,
  41. intRandom: intRandom,
  42. greetAll: greetAll,
  43. sum: sum,
  44. }
  45. let result = objFunction[prompt("Введите название задания").toLowerCase()]()