Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daubattu/9e1f01cabb5e01573e9542ae57bf9ac4 to your computer and use it in GitHub Desktop.
Save daubattu/9e1f01cabb5e01573e9542ae57bf9ac4 to your computer and use it in GitHub Desktop.
//[Hackerrank] Solution of Modified Kaprekar Numbers in JavaScript - nguyenhungkhanh.com
//[Hackerrank] Solution of Modified Kaprekar Numbers in JavaScript
function kaprekarNumbers(p, q) {
let result = [];
for(let i = p; i <= q; i++) {
const squareString = (i * i).toString();
const num1 = squareString.substring(0, squareString.length/2);
const num2 = squareString.substring(squareString.length/2, squareString.length);
if (Number(num1) + Number(num2) === i) {
result = result.concat(i)
}
}
if (result.length === 0) {
console.log('INVALID RANGE')
} else {
console.log(...result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment