Frog hopping program: Premise: A frog wants to jump from one end of the shore(X)
ID: 3667807 • Letter: F
Question
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. I was given an object (Obj), which says where the next leaf would fall, minimum frog jumping distance, starting position X, and destination Y.
I am 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.
For example,
Obj = {0:1,1:3,2:6,3:4,4:5,5:0,6:2,7:-1}, D=3, X=0 and Y=7
function frogHop(obj, D,X,Y)
{
// my code
}
frogHop(Obj,D, X,Y); // Invoking the function.
Explanation / Answer
Frog hopping program: class Solution { // X=start, Y=end, D=distance for code clarity public int solution(int start, int end, int distance) { // write your code in Java SE 7 int progress = start; int count = 0; while (progressRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.