Sum of positive codewars javascript

Permalink

Cannot retrieve contributors at this time

You get an array of numbers, return the sum of all of the positives ones.

Example [1,-4,7,12] => 1 + 7 + 12 = 20

Note: array may be empty, in this case return 0.

function positiveSum(arr) {
  return arr.reduce((prev, current) => {
    if (current > 0) {
      return prev + current;
    }

    return prev
  }, 0)
}

  1. Sum using reduce who start a 0
  2. if the current is bigger than 0 than mean is position so add the value to the prev one
  3. if not return the prev

Chek kata on Codewars

Description:

You get an array of numbers, return the sum of all of the positives ones.

Example [1,-4,7,12] => 1 + 7 + 12 = 20

Note: if there is nothing to sum, the sum is default to 0.

Solution 1

Let's use reduce() method to solve this task. It executes a reducer function (that you provide) on each element of the array, resulting in single output value. It is almost a textbook example for reducer.

function positiveSum(arr) {
  const reducer = (accumulator, current) => accumulator + (current > 0 ? current : 0)
  return arr.reduce(reducer, 0)
}

There are a few variations of this solution. The main difference is how we check if the number is positive or not.

Solution 1.1

const positiveSum = arr => arr.reduce((accumulator, current) => current > 0 ? accumulator + current : accumulator, 0);

Solution 1.2

const positiveSum = arr => arr.filter(number => number > 0).reduce((accumulator, current) => accumulator + current, 0)

Solution 1.3

const positiveSum = arr => arr.reduce((accumulator, current) => accumulator + Math.max(current, 0), 0)

Solution 2

For loop will do the trick as well.

function positiveSum(arr) {
    let sum = 0;
    for(let i = 0; i < arr.length; i++) {
        if(arr[i] > 0) sum += arr[i]
    }
    return sum
}

Solution 2.1

Or forEach loop also works fine.

function positiveSum(arr) {
  let sum = 0
  arr.forEach(num => num > 0 && (sum += num))
  return sum
}

  • Sign Up

    Time to claim your honor

  • Training
  • Practice

    Complete challenging Kata to earn honor and ranks. Re-train to hone technique

  • Freestyle Sparring

    Take turns remixing and refactoring others code through Kumite

  • Career
  • Opportunities

    Find your next career challenge – powered by Qualified Jobs

  • Community
  • Leaderboards

    Achieve honor and move up the global leaderboards

  • Chat

    Join our Discord server and chat with your fellow code warriors

  • Discussions

    View our Github Discussions board to discuss general Codewars topics

  • About
  • Docs

    Learn about all of the different aspects of Codewars

Sum of positive

  • Details
  • Solutions
  • Discourse (217)

Description:

Loading description...

Similar Kata:

Stats:

Created Apr 19, 2016
Published Apr 19, 2016
Warriors Trained 227094
Total Skips 9284
Total Code Submissions 413311
Total Times Completed 180161
JavaScript Completions 75578
Python Completions 49664
Ruby Completions 5427
C# Completions 10537
CoffeeScript Completions 58
Haskell Completions 1595
C Completions 4238
TypeScript Completions 1790
PHP Completions 4313
C++ Completions 6746
Go Completions 3232
Java Completions 16029
Elixir Completions 414
NASM Completions 76
Crystal Completions 35
Groovy Completions 92
Scala Completions 614
Kotlin Completions 1489
OCaml Completions 116
Swift Completions 2121
Rust Completions 1440
Racket Completions 70
Julia Completions 86
PowerShell Completions 297
Reason Completions 11
Dart Completions 751
Lua Completions 276
Clojure Completions 162
Factor Completions 14
R Completions 96
COBOL Completions 9
D Completions 11
RISC-V Completions 6
Total Stars 1326

% of votes with a positive feedback rating 93% of 15940
Total "Very Satisfied" Votes 13776
Total "Somewhat Satisfied" Votes 1976
Total "Not Satisfied" Votes 188