DEV Community

Naya Willis
Naya Willis

Posted on

Hacker Rank: Compare the Triplets

Hacker Rank Challenge - Compare the Triplets

Problem:
Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.

The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
If a[i] > b[i], then Alice is awarded 1 point.
If a[i] < b[i], then Bob is awarded 1 point.
If a[i] = b[i], then neither person receives a point.

My Solution

function compareTriplets(a, b) {
    const scoreBoard = [0, 0]
    let i = 0
    while (i < a.length) {
        if (a[i] > b[i]) {
            scoreBoard[0] += 1
        } else if (a[i] < b[i]) {
            scoreBoard[1] += 1
        }
        i++
    }
    return scoreBoard
}
Enter fullscreen mode Exit fullscreen mode
  1. I created a scoreBoard and set it equal to [0, 0], each element representing both Alice’s and Bob’s points initially.
  2. I created a counter and while loop that uses that counter.
  3. I created an if else if conditional.
  4. If a[i] Alice’s criteria score is greater than b[i] Bob’s criteria score, then we increment Alice’s final score by 1 which is scoreBoard[0], otherwise if Bobs criteria score is greater than Alice’s then we increment Bob’s final score by 1.

Discussion (2)

Collapse
bugb profile image
bugb • Edited

I would do something like this:

const compareTriplets=(a,b)=>a.reduce((m,n,i)=>[m[0]+(n>b[i]),m[1]+(b[i]>n)],[0,0])
Enter fullscreen mode Exit fullscreen mode

It used coercion in javascript so we do not need to use if-else here

FYI It passed all test cases!

Collapse
greedybrain profile image
Naya Willis Author

sick lol