Skip to content

Instantly share code, notes, and snippets.

@skylerto
Created April 21, 2016 15:30
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 skylerto/c1f137bf88079e7d84b99f803176bd2c to your computer and use it in GitHub Desktop.
Save skylerto/c1f137bf88079e7d84b99f803176bd2c to your computer and use it in GitHub Desktop.
function solution(A, B) {
if (A == B){
// Same values
return 0;
} else if(A > 0 && B > 0){
// Positive values
var left = Math.sqrt(A);
var right = Math.sqrt(B);
return (Math.floor(right) - Math.floor(left)) + 1;
} else if(A < 0) {
// A is less than 0.
var left = Math.sqrt(-1 * A);
var right = Math.sqrt(B);
return (Math.floor(right) + Math.floor(left)) + 1;
} else {
// Both are less than 0 (A<=B).
var left = Math.sqrt(-1 * A);
var right = Math.sqrt(-1 * B);
return (Math.floor(right) - Math.floor(left)) + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment