Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Output: 56(2^2 + 4^2 + 6^2 = 4 + 16 + 36 = 56)
- /*
- [2, 4, 6]
- [4, 16, 36]
- 56
- */
- //Time complexity (n)
- //space complexity (n)
- //Sum of Event Square
- // function sumOfEvenSquare(inputArr) {
- // Define a array to track even numbers
- // const evenArr = []
- // Define array to track down the squared number
- // const squaredArr = []
- // Define a variable to track down the sum
- // let total = 0
- // find out the even numbers
- // for (let elm of inputArr) {
- // console.log(elm)
- // if (elm % 2 === 0) {
- // evenArr.push(elm)
- // }
- // }
- // Squared the even numbers and return an array
- // for (let elm of evenArr) {
- // squaredArr.push(elm * elm)
- // }
- // sum of the squared number array
- // for (let elm of squaredArr) {
- // total += elm
- // }
- // return the result
- // return total
- // }
- //Sum of Event Square
- function sumOfEvenSquare(inputArr) {
- //find out the even numbers and squared and sum of those numbers
- const total = inputArr
- .filter((elm) => elm % 2 === 0)
- .map((elm) => elm * elm)
- .reduce((acc, elm) => acc + elm)
- //return the result
- return total
- }
- console.log(sumOfEvenSquare([1, 2, 4, 6, 7]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement