We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
const range = (a, b) => {
let arr = [];
for (a; a <= b; a++) {
arr.push(a)
}
return arr;
};
function kaprekarNumbers(p, q) {
// Write your code here
const res = []
for(let num of range(p, q)){
console.log(num)
let sqr = num*num;
let str_sqr = sqr.toString();
let d = num.toString().length;
let r = str_sqr.slice(-d);
let l = str_sqr.slice(undefined,-d) || '0';
if((parseInt(r) + parseInt(l)) === num){
res.push(num)
}
}
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
Simpl JavaScript solution:
const range = (a, b) => { let arr = []; for (a; a <= b; a++) { arr.push(a) } return arr; };
function kaprekarNumbers(p, q) { // Write your code here const res = [] for(let num of range(p, q)){ console.log(num) let sqr = num*num; let str_sqr = sqr.toString(); let d = num.toString().length; let r = str_sqr.slice(-d); let l = str_sqr.slice(undefined,-d) || '0'; if((parseInt(r) + parseInt(l)) === num){ res.push(num) } }
}