Open In App

How to Iterate Through Numbers and Return a String Message if the Sum is Negative?

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, iterating through numbers or any kind of iterable data structures is a common programming task. The ability to repeat a certain block of code multiple times allows you to perform operations on a set of data. We can iterate through an array of numbers and return a string message if the sum of the numbers is negative.

Using JavaScript For Loop

The for loop in JavaScript is a control flow statement that allows you to repeatedly execute a block of code based on a specified condition. The for loop has three parts: initialization of a counter variable, the condition itself, and the update section.

Syntax:

for(initialization; condition; updation){}

Example: The below code uses a JavaScript function with a for loop to iterate through numbers and return a string message if the sum is negative.

Javascript




function sumChecker(numbers) {
    let sum = 0;
    for (let i = 0; i < numbers.length; i++) {
        sum += numbers[i];
    }
    if (sum < 0) {
        return `The sum of the numbers is ${sum}, and is negative.`;
    } else {
        return `The sum of the numbers is ${sum}, and is positive.`;
    }
}
 
console.log(sumChecker([1, -2, 3, -4, -5]));
console.log(sumChecker([1, 4, 5, -3, 2]));


Output

The sum of the numbers is -7, and is negative.
The sum of the numbers is 9, and is positive.

Using JavaScript For Of Loop

The for of loop in JavaScript provides a concise and easy-to-read syntax for iterating through the values of an iterable, eliminating the need for explicit index management. This loop has namely two parts: the variable which is assigned the value of each element of the iterable on each iteration, and the iterable itself.

Example: The below code uses a JavaScript function with a for of loop to iterate through numbers and return a string message if the sum is negative.

Javascript




function sumChecker(numbers) {
    let sum = 0;
    for (num of numbers) {
        sum += num;
    }
    if (sum < 0) {
        return `The sum of the numbers is ${sum}, and is negative.`;
    } else {
        return `The sum of the numbers is ${sum}, and is positive.`;
    }
}
 
console.log(sumChecker([1, -2, 3, -4, -5]));
console.log(sumChecker([1, 4, 5, -3, 2]));


Output

The sum of the numbers is -7, and is negative.
The sum of the numbers is 9, and is positive.

Using JavaScript While Loop

The while loop in JavaScript is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition evaluates to true.

Syntax:

while(consitionalExpression){
// JavaScript statements with incrementor
}

Example: The below code uses a JavaScript function with the while loop to iterate through numbers and return a string message if the sum is negative.

Javascript




function sumChecker(numbers) {
    let sum = 0;
    let index = 0;
    while (index < numbers.length) {
        sum += numbers[index];
        index++;
    }
    if (sum < 0) {
        return `The sum of the numbers is ${sum}, and is negative.`;
    } else {
        return `The sum of the numbers is ${sum}, and is positive.`;
    }
}
 
console.log(sumChecker([1, -2, 3, -4, -5]));
console.log(sumChecker([1, 4, 5, -3, 2]));


Output

The sum of the numbers is -7, and is negative.
The sum of the numbers is 9, and is positive.

Using JavaScript Array forEach() Method

The forEach() method is a higher-order function available for arrays. It allows you to iterate over the elements of an array and perform a specified operation by passing a callback function to operate on each element.

Syntax:

arrayName.forEach(()=>{});

Example: The below code uses a JavaScript function with the Array forEach() method to iterate through numbers and return a string message if the sum is negative.

Javascript




function sumChecker(numbers) {
    let sum = 0;
    numbers.forEach(num => {
        sum += num;
    })
    if (sum < 0) {
        return `The sum of the numbers is ${sum}, and is negative.`;
    } else {
        return `The sum of the numbers is ${sum}, and is positive.`;
    }
}
 
console.log(sumChecker([1, -2, 3, -4, -5]));
console.log(sumChecker([1, 4, 5, -3, 2]));


Output

The sum of the numbers is -7, and is negative.
The sum of the numbers is 9, and is positive.

Using JavaScript Array reduce() Method

The Javascript Array reduce() method is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.

Syntax:

arrayName.reduce(()=>{});

Example: The below code uses a JavaScript function with the Array reduce() method to iterate through numbers and return a string message if the sum is negative.

Javascript




function sumChecker(numbers) {
    const sum = numbers.reduce((acc, num) => acc + num, 0);
    if (sum < 0) {
        return `The sum of the numbers is ${sum}, and is negative.`;
    } else {
        return `The sum of the numbers is ${sum}, and is positive.`;
    }
}
 
console.log(sumChecker([1, -2, 3, -4, -5]));
console.log(sumChecker([1, 4, 5, -3, 2]));


Output

The sum of the numbers is -7, and is negative.
The sum of the numbers is 9, and is positive.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads