Basic JavaScript: Use the parseInt Function

Here are my instructions: Use parseInt() in the convertToInteger function so it converts the input string str into an integer, and returns it.

Here is my set up:

function convertToInteger(str) {


Here is my solution that doesn't work:

function convertToInteger(str) {
var a = parseInt("007");


Please help. Thanks.

1 Like

Two things, your function is missing a return statement, so it is not returning anything
And parseInt needs a radix, you may want to reread how it works

Ok, three things - your function has a parameter, str, that you are not using, so your function is not reusable and do always the same thing independently from the argument passed in

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Just to clarify this point, parseInt is capable of automatically determining the radix depending on context. I am not experienced to know how well this works in every case, but I think it would be safe to assume that converting number strings to number integers could be handled easily. This MDN article about parseInt states:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • If the input string begins with “0x” or “0X”, radix is 16 (hexadecimal) and the remainder of the > string is parsed.
  • If the input string begins with “0”, radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt .
  • If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN .

Edit: MDN clearly states radix should always be defined. This is why you research before you post! :sweat_smile:

1 Like

After having trouble myself and reading what was posted here, I tried this and it worked for me:

function convertToInteger(str) {
var a = parseInt(str);
return a;


Hope this helps!


This helped me thank you. But I would like to now why it worked. In my code I had left out the ‘return a’ - why does it have to be ‘a’, could it be any other letter? Also why is it that the ‘convertToInterger’ is after the return and not before it? Thanks in advance.

you can certainly avoid the declaration of variable a and just return the value from the parseInt() method. If you assign the value returned from parseInt() to a variable, the variable can be named in any way you want, it just needs to be the one you return

If you mean the convertToInteger("56") written at the end, that is the code that tell to run the function with the value "56" passed in. If you see it is outside the function body (after the closing “}” ) If you don’t call the function after creating it, it is not executed