Everyone Can Code!

JavaScript Kata #8: Sum of positive

By YayoDrayber | Learn To Code With Me | 10 Aug 2022


If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.

If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #8

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.

Starting code:

function positiveSum(arr) {
  
}

My attempt:

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

Fudged a little bit because I had a typo with i=o, apparently pressed the letter 'o' instead of a zero and only saw the typo after I tried to run it against the tests.

Ranked #1 'Best Practice' with 814 votes and 138 'Clever' votes at the time of writing:

function positiveSum(arr) {
  var total = 0;    
  for (i = 0; i < arr.length; i++) {    // setup loop to go through array of given length
    if (arr[i] > 0) {                   // if arr[i] is greater than zero
      total += arr[i];                  // add arr[i] to total
    }
  }
  return total;                         // return total
}

Huh, would you look at that. Never thought it'd be so soon that my approach would be beside others who were voted #1. But I'm unsatisfied because I was trying to apply the previously learned .reduce method but I couldn't figure out how to insert the conditional so I was glad when I saw how it was done with rank #2, receiving 527 votes and 1398 'Clever' votes at the time of writing:

function positiveSum(arr) {
   return arr.reduce((a,b)=> a + (b > 0 ? b : 0),0);
}

Also learned how to use the .filter method with rank #3 'Best Practice':

function positiveSum (arr) {
  return arr.filter(x => x>=0).reduce((a, c) => a + c, 0);
}

Rank #6 also taught me that the .map method combined with a ternary can be used as well, kudos to andorey the one user who submitted this:

function positiveSum( obj ) {
  return obj.map(el => el < 0 ? 0 : el).reduce((acc, i)=> acc + i, 0)
}

That's it for Kata #8, stay tuned for more katas to be solved!

Link to Kata #1: Square(n) Sum

Link to Kata #2: Convert a Number to a String

Link to Kata #3: DNA to RNA Conversion

Link to Kata #4: Remove First and Last Character

Link to Kata #5: MakeUpperCase

Link to Kata #6: Total amount of points

Link to Kata #7: A Needle in the Haystack

How do you rate this article?

4


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.