Skip to content

Instantly share code, notes, and snippets.

@andhikamaheva
Created July 3, 2018 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andhikamaheva/77e4a4bc393b594aa17856528fbe9764 to your computer and use it in GitHub Desktop.
Save andhikamaheva/77e4a4bc393b594aa17856528fbe9764 to your computer and use it in GitHub Desktop.
Codility FrogJmp Exercise Solution (JavaScript/NodeJS)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(X, Y, D) {
// write your code in JavaScript (Node.js 8.9.4)
if (X >= Y) {
return 0
} else if (X === Y) {
return 0
} else if (D >= (Y-X)) {
return 1
} else {
minimalJumps = parseInt((Y-X) / D)
minimalJumps += ((Y-X)%D > 0 ? 1 : 0)
return minimalJumps
}
}
@yamankatby
Copy link

A faster and cleaner solution

function solution(X, Y, D) {
  return Math.ceil((Y - X) / D);
}

@SalahAdDin
Copy link

@yamankatby Yeah, you are right!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment