Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Javascript Frog hopping program: Premise: A frog wants to jump from one end of t

ID: 3667892 • Letter: J

Question

Javascript

Frog hopping program:

Premise: A frog wants to jump from one end of the shore(X) to the other end of the shore (Y), but there

was a river between them. However, the frog can reach another end of the shore by jumping on the falling

leaves. The frog can jump only fixed distance (D). It cannot jump from one shore to the other in a single

jump if Y-X>D

Write a javascript program that returns a minimum number of hops to reach from one shore to the other.

You will be given an object (Obj), which says where the next leaf would fall, minimum frog jumping

distance, starting position X, and destination Y.

For example: Let’s say frog can jump a minimum distance (D) of 3. X=0 and Y=7

Minimum Hop: 4 (1->3 ->4>7)

Explanation: When the frog is at position “0”, a leaf is falling at position 1. So the frog can jump to 1

because abs(1-0) <=3, where 3 refers minimum hopping distance (D) of the frog. Now frog is at position

1. In the above table, when the frog is at position “1”, a leaf is falling at position 3. Can the frog jump to

position 3 from current position (position 1)? Yes. Because abs(3-1)<=3. Now frog jumped to position 3.

When the frog is at position 3, a leaf is falling at position 4. Can the frog jump from position 3 to position

4? Yes because abs(4-3)<=3. The frog is at position 4. When the frog is at position 4, a leaf is falling at

position 5. Can the frog jump from position 4 to position 5? Yes but it is not required. The objective is to

reach 7 in a minimum number of hops. Here 7 is another end of the shore. Now the frog is at position 4

and destination is at position 7. Here abs(7-4) <=3 is true. Hence, from position 4 the frog can do a single

jump and reach the destination (7).

Another solution (not ideal): 5 (1->3 ->4->5->7)

You are required to write a JavaScript function, which takes four arguments:

1. Frog hopping object “Obj.”

2. the minimum distance that a frog can jump (D)

3. Starting Position: X

4. Ending Position: Y

Return value: Minimum hops to reach Y or Display frog can’t reach the destination.

Explanation / Answer

Answer:

frog.js

Obj = {0:1,1:3,2:6,3:4,4:5,5:0,6:2,7:-1};
D=3;
X=0;
Y=7;

function HoppingFrog(obj, D,X,Y)
{
var a=[];
for(var i in obj){
a[i]=obj[i];

}


var current=X;
var jumps=0;
var NoOfJumps=[];
flag=1;
while (Math.abs(current-Y)>D)
{
NoOfJumps.push(current);
current=a[current];
if (NoOfJumps.indexOf(current)!=-1)
{
flag=0;
break;
}
jumps+=1;
}
if (flag==1)
{
if (current==Y)
return "The number of jumps required " + jumps;
else
jumps++;
return "The number of jumps required " + jumps;
}

return "Out Of reach";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote