DEV Community

Naya Willis
Naya Willis

Posted on

1 1

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.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (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

sick lol

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️