Log in

Number base

  • 1
  • 20 Apr

My code can run smoothly 10 example but, I don't know why its stuck at example 11. Need help!

function convert(strNumber, radix) {
var a = parseInt(strNumber, radix);
if(isNaN(a))
return -1;
else
return a;
}
Untitled.png

  • 1
  • 21 Apr

The subtlety of the parseInt() JS function you are using is that "If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point." The test problem here requires different behavior, that any non-valid character anywhere in the string should lead to a NaN and thus -1.

More concretely,
parseInt("abc",10) returns NaN, but parseInt("734abc",10) returns 734. And
parseInt("SAD", 15) returns NaN but parseInt("ASD", 15) returns 10.

You either need to avoid parseInt() or change the way you are using it.

Reply