Advertisement
samimwebdev

Problem Solving Approach

Aug 30th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Output: 56(2^2 + 4^2 + 6^2 = 4 + 16 + 36 = 56)
  2. /*
  3. [2, 4, 6]
  4. [4, 16, 36]
  5. 56
  6. */
  7. //Time complexity (n)
  8. //space complexity (n)
  9. //Sum of Event Square
  10. // function sumOfEvenSquare(inputArr) {
  11. //   Define a array to track even numbers
  12. //   const evenArr = []
  13. //   Define array to track down the squared number
  14. //   const squaredArr = []
  15. //   Define a variable to track down the sum
  16. //   let total = 0
  17.  
  18. //  find out the even numbers
  19. //   for (let elm of inputArr) {
  20. //     console.log(elm)
  21. //     if (elm % 2 === 0) {
  22. //       evenArr.push(elm)
  23. //     }
  24. //   }
  25. //   Squared the even numbers and return an array
  26. //   for (let elm of evenArr) {
  27. //     squaredArr.push(elm * elm)
  28. //   }
  29. //   sum of the squared number array
  30. //   for (let elm of squaredArr) {
  31. //     total += elm
  32. //   }
  33. // return the result
  34. //   return total
  35. // }
  36.  
  37. //Sum of Event Square
  38. function sumOfEvenSquare(inputArr) {
  39.   //find out the even numbers and squared and sum of those numbers
  40.   const total = inputArr
  41.     .filter((elm) => elm % 2 === 0)
  42.     .map((elm) => elm * elm)
  43.     .reduce((acc, elm) => acc + elm)
  44.  
  45.   //return the result
  46.   return total
  47. }
  48. console.log(sumOfEvenSquare([1, 2, 4, 6, 7]))
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement