Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}`);