Everyone Can Code!

JavaScript Kata #6: Total amount of points


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 #6

DESCRIPTION:

Our football team finished the championship. The result of each match look like "x:y". Results of all matches are recorded in the collection.

For example: ["3:1", "2:2", "0:1", ...]

Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match:

  • if x > y: 3 points
  • if x < y: 0 point
  • if x = y: 1 point

Notes:

  • there are 10 matches in the championship
  • 0 <= x <= 4
  • 0 <= y <= 4

Starting code:

function points(games) {
  // your code here
}

My attempt that worked:

function points(games) {
  // your code here
  let sumOfPoints = 0
  for(i = 0; i < games.length ; i++){
    let gamesStrHolder = games[i]
    let x = gamesStrHolder[0]
    let y = gamesStrHolder[2]
    if (x > y){
      sumOfPoints += 3
    }else if (x == y){
      sumOfPoints += 1
    }else if (x < y){
      sumOfPoints += 0
    }
  }
  return sumOfPoints
}

Got way too excited and attempted to use the .split and .substring methods I learned from before then I realized I needed to pick out individual characters from out of the string within the array which is why I ended up with my solution above. Got a little bit stuck on the else if part because what I meant to do was a comparison (x == y) but forgot the second equal sign so I ended up with reassignment which is definitely a wrong statement.

Ranked #1 in 'Best practices' goes to:

const points=games=>games.reduce((output,current)=>{
    return output += current[0]>current[2] ? 3 : current[0]===current[2] ? 1 : 0;
  },0)

// function points(games) {
//   return games.reduce((output,current)=>{
//     let x = parseInt(current[0]);
//     let y = parseInt(current[2]);
//     let value= x>y ? 3 : x===y ? 1 : 0;
//     return output+value;
//   },0)
// }

'Doh moment. I got got. Only when I saw this did I realize, "oh that's right, a .reduce method could've been used" but it's too late now. 😅

Best Practice rank #2:

function points(games) {
  let total = 0;
  games.map(game => {
    if (game[0] === game[2]) {
      total += 1;
    } else if (game[0] > game[2]) {
      total += 3;
    }
  });
  return total;
}

I only started to learn about the .map method so I didn't know it could have been used here... and now I know. 😂

Best Practice rank #3:

function points(games) {
  var sum=0;
  for (var i=0; i<games.length; ++i)
  {
    if (games[i][0]>games[i][2])
      sum+=3;
    if (games[i][0]==games[i][2])
      sum+=1;
  }
  return sum;
}

This looks a lot like my solution but skipped all the extra variable declarations and went straight to referencing the deeper level index which is a pretty cool thing to learn.

That's it for Kata #6, 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

How do you rate this article?

3


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.