Hello everyone; I have been grappling with 3 cases that are not passing. The difference is so subtle with a digit change. I tried JavaScript notations for very large numbers or scientific notations but of of no avail. The challenge is linked here.
The different test cases are here.
console.log(separateNumbers("90071992547409929007199254740993")) //Gives NO although it should give "yes"
console.log(separateNumbers("90071992547401929007199254740193")) //Gives YES when 9 replaced with 1//
My code goes like:
function separateNumbers(s) {
var beautiful = true;
for (let len = 1; len < s.length; len++) {
var first = s.substr(0, len);
var num = s.substr(0, len);
if (s.length <= len) {
continue;
}
var sNew = ''.concat(first.toString());
while (sNew.length < s.length) {
num++;
sNew = sNew.concat(num.toString());
}
if (sNew === s) {
console.log('YES ' + first);
beautiful = false;
continue;
}
}
if (beautiful) {
console.log('NO')
}
}
Your helps are very much appreciated. Thanks many
There is no issue with your algorithm, it is working exactly as expected (and is right, btw).
The issue is with JS-Number data-type. The MAX_SAFE_INTEGER constant has a value of 9007199254740991. The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1
Instead of using std Number type, use BIGINT. That should give you correct answer for the test case.