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.
I have tried it first so that it passes the Hackerrank tests (with 'INVALID RANGE' and console.log). Then I thought that doing it without INVALID RANGE and console.log could be a good opportunity to practise recursion. Having said that, JSBench says it's slower at times and faster when I try it again...I guess it isn't always worth it :( hahaha:
Withrecursion:constkaprekarNumbers=(p,q)=>p===q?`${p===parseInt((p*p).toString().slice(((p*p).toString().length/2),((p*p).toString().length)),10)+parseInt((p*p).toString().slice(0,((p*p).toString().length/2)),10)||p===1?p:""}`:`${p===parseInt((p*p).toString().slice(((p*p).toString().length/2),((p*p).toString().length)),10)+parseInt((p*p).toString().slice(0,((p*p).toString().length/2)),10)||p===1?p+" ":""}`+kaprekarNumbers((p+1),q)Without:functionkaprekarNumbers(p,q){// Write your code hereletfinalNums=""letnum;for(p;p<=q;p++){num=pif(num===parseInt((num*num).toString().slice(((num*num).toString().length/2),((num*num).toString().length)),10)+parseInt((num*num).toString().slice(0,((num*num).toString().length/2)),10)||num===1)finalNums=finalNums+num+" "}returnfinalNums.length>0?console.log(finalNums):"INVALID RANGE"}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
I have tried it first so that it passes the Hackerrank tests (with 'INVALID RANGE' and console.log). Then I thought that doing it without INVALID RANGE and console.log could be a good opportunity to practise recursion. Having said that, JSBench says it's slower at times and faster when I try it again...I guess it isn't always worth it :( hahaha: