ParseInt Function with a Radix

Tell us what’s happening:

I have tried finding a solution with/out the radix. When I console.log with base 2, I am getting 19 but my test still returns an error like so;

function convertToInteger(str, radix) {
  var parsed = parseInt(str, radix)
  if (isNaN(parsed)) {
    return NaN;  
  return parsed;

console.log(convertToInteger("10011", 2));

returns 19 before running the test.

and it returns this;

// running tests

convertToInteger("10011") should return 19

convertToInteger("111001") should return 57

// tests completed

after running the test. 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:

This is the function call done by the tests, only one argument

Your function has two parameters - so your radix is actually undefined

Anyway, you don’t need the part with NaN as parseInt returns NaN on its own if it can’t convert to a number

I have tried with one argument as well and I am getting nowhere. How do I get convertToInteger() to return 19 and 57.

NaN (convertToInteger("JamesBond") should return NaN) is one of the test required and it passes.

Its the two tests i.e. convertToInteger("10011") should return 19 and convertToInteger("111001") should return 57 that are not passing.

Look at this line, what should radix be? It can’t be a parameter of the function because the function calls in the tests have only one argument, so you need to actually put a value there instead of a variable

If you use parseInt correctly, it will return NaN on its own without needing other lines of code, as the value returned from parseInt would be NaN

yes i see what you say about NaN. Let me try var parsed = parseInt(str, radix)

parseInt() method will return NaN if string argument is not number so you don’t need to test the result.
about radix, either you put 2 as parameter in parseInt() method or create a var radix = 2

I put in a value and I got all my tests to pass. I can now move. Thanks,