Solving Hacker Rank ‘Number Line Jumps’

Danielle Torres
2 min readApr 18, 2021

--

Hi Everyone,

Today I will be discussing how I solved Hacker Rank Challenge, Number Line Jump, in JavaScript (level: easy).

Objective: You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (.i.e. toward positive infinity);

  • The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump.
  • The second kangaroo starts at location x2 and moves at a rate of v2 meters.

You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.

The Challenge is providing a starting position for both kangaroos (x1, x2) and the rate they are moving (v1, v2)

More information below:

I begin by creating three new variables.

let kangaPos1 = x1; (kangaroo 1 starting position)

let kangaPos2 = x2; (kangaroo 2 starting position)

let i = 0;

I create a for loop that will loop until iterator is less greater 10000 the same as displayed in the constraint.

If Kangaroo 1’s Position (kangaPos1) is equal to Kangaroo 2’s Position (kangaPos2) then we want to return YES else increment by their jump distance.

If the condition is never met before the for loop completes NO will return as the kangaroos never in the same location.

function kangaroo(x1, v1, x2, v2) {let kangaPos1 = x1;
let kangaPos2 = x2;
let i = 0;
for (i=0; i < 10000 ; i++) {if ( kangaPos1 === kangaPos2 ) {
return ‘YES’} else {
kangaPos1 += v1;
kangaPos2 += v2;};
};
return ‘NO’
};

It Passed!

Please let me know if this helps you with your challenge or if you like the content! Until next time!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Danielle Torres
Danielle Torres

Written by Danielle Torres

Highly organized & motivated Software Engineer with an application analyst/data analyst background.

No responses yet

Write a response