Skip to content

Instantly share code, notes, and snippets.

@alexko30
Created April 30, 2018 22:34
Show Gist options
  • Save alexko30/705b9f862cf8815262f6e9c89991dfdc to your computer and use it in GitHub Desktop.
Save alexko30/705b9f862cf8815262f6e9c89991dfdc to your computer and use it in GitHub Desktop.
sum Of Positive And Negative Elements
function sumOfPositAndNegatElem(arr) {
let positiveSum = 0;
let negativeSum = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0 || arr[i] == 0) {
negativeSum += arr[i];
} else if (arr[i] > 0) {
positiveSum += arr[i];
}
}
console.log(arr);
console.log(`Positive sum = ${positiveSum}, and negative sum = ${negativeSum}`);
}
sumOfPositAndNegatElem([222, -90, 10, -8]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment