Created
January 10, 2020 08:40
-
-
Save daubattu/9e1f01cabb5e01573e9542ae57bf9ac4 to your computer and use it in GitHub Desktop.
//[Hackerrank] Solution of Modified Kaprekar Numbers in JavaScript - nguyenhungkhanh.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[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