Sprint 2 - Algorithmic Efficiency Homework(Javascript)
Homework for the Algorithmic Effiency in Javascript
- JS Algorithmic Efficiency Activity: Sum of Even Numbers
- 🎯 Goal
- 🧩 Instructions
- 💡 Example
JS Algorithmic Efficiency Activity: Sum of Even Numbers
🎯 Goal
Write a function that takes an array of numbers and calculates the sum of all even numbers. After completing the function, analyze the time and memory complexity of your solution.
🧩 Instructions
- Complete the function
sumEvenNumbers(arr)in the next code cell. - Test it with arrays of different sizes.
- Think about:
- Time Complexity: How long it takes to compute as the array grows.
- Memory Complexity: How much extra memory your algorithm uses.
- Optional: Try to optimize memory usage if possible (e.g., avoid creating extra arrays).
💡 Example
// Input: [1,2,3,4,5]
// Output: 6 (because 2 + 4 = 6)
// Starter Code to Build Off of:
// Task: Complete this function
function sumEvenNumbers(arr) {
// Your code here
}
// Test your function
const testArray = [1, 2, 3, 4, 5];
console.log("Sum of even numbers:", sumEvenNumbers(testArray));
// Optional: test with larger arrays
const largeArray = Array.from({length: 10}, (_, i) => i + 1);
console.log("Sum of even numbers in large array:", sumEvenNumbers(largeArray));