diff --git a/index.js b/index.js index ff28a6f9..8cc4376c 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,31 @@ -class NumberArray { - constructor(numbers) { - this.numbers = numbers; - } +// Functional approach to calculate the sum of positive numbers in an array + +/** + * Function to calculate the sum of positive numbers in an array. + * @param {Array} numbers - The array of numbers. + * @returns {number} - The sum of positive numbers. + */ +function getPositiveSum(numbers) { + // Initialize the sum + let sum = 0; - getPositiveSum() { - let sum = 0; - for (let number of this.numbers) { - if (number > 0) { - sum += number; - } + // Loop through each number in the array + for (let number of numbers) { + // Check if the number is positive + if (number > 0) { + // Add the positive number to the sum + sum += number; } - return sum; } + + // Return the sum of positive numbers + return sum; } +// Test the function with example input const numbers = [2, -4, 6, -8, 10, -12]; -const numberArray = new NumberArray(numbers); -const positiveSum = numberArray.getPositiveSum(); +const positiveSum = getPositiveSum(numbers); -// Input: [2, -4, 6, -8, 10, -12] -// Output: 18 +// Print the input and output +console.log(`Input: [${numbers.join(', ')}]`); +console.log(`Output: ${positiveSum}`);