DEV Community

Cover image for HackerRank — Problem Solving — JavaScript — Compare the Triplets
Abu Saleh Faysal
Abu Saleh Faysal

Posted on

HackerRank — Problem Solving — JavaScript — Compare the Triplets

Image description

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.

Comparison points is the total points a person earned.Given a and b, determine their respective comparison points.

Solution:

Image description

Explanation:

  • Step 01: Take two variables named "first" and "second". Then store an initial value as 0 for both of them.

  • Step 02: Iterate a for loop through the given array(the array can be the first one or the second one, as long as they are equal in length, we can use any of them).

  • Step 03: If the element of first array is greater the second array, we will increment the "first" variable by 1. However, if the element of second array is greater the first array, we will increment the "second" variable by 1. Lastly, in case the the elements of both array contain the same value, we will not make any changes.

  • Step 04: Return the "first" and "second" variables in the form of an array where first element is the "first" variable and the second element is the "second" variable.

Top comments (0)