parseInt function: Confused about terminology

Here’s the FCC challenge. I’ve already passed it, but I’m trying to understand it better before I move on:

The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.

The function call looks like:

parseInt(string, radix);

And here's an example:

var a = parseInt("11", 2);

The radix variable says that "11" is in the binary system, or base 2. This example converts the string "11" to an integer 3.

Use parseInt() in the convertToInteger function so it converts a binary number to an integer and returns it.

function convertToInteger(str) {
  
}

convertToInteger("10011"); 

So here are my questions:

  1. If “string” in JavaScript means “text,” why is our string a number?

  2. Why does our number, 10011, need to be parsed? 10011 is already a whole number.

1 Like
  1. If “string” in JavaScript means “text,” why is our string a number?

Strings may get confusing when thinking of it as just text. Strings are sequences of characters. The characters can be letters (A-Z,a-z) numbers (0-9) or other symbols (! . , $ # etc.). Strings are generally surrounded by quotation marks (“” or ‘’) . So for a number, say 1, it can be a number value but it can also be character value. When 1 is in quotation marks it is interpreted as a string (“1”). You can test this by opening up the browser console and typing typeof 1; and typeof "1";. The first will say it’s a “number” and the second will say it is a “string”.

  1. Why does our number, 10011, need to be parsed? 10011 is already a whole number.

First, it has to parsed because it is a string of characters, namely 1’s and 0’s. This string has to be interpreted as a number rather than a string. Second, 10011 may be the whole number ten thousand and eleven or it may represent a binary number and actually be equivalent to 19. The function not only says what number to interpret but also how to interpret it.

3 Likes

Furthermore may I advice you do not look at a string as

but that you see it as a type of data (see data types [string, integer, float, array, object]) and a primitive form of data (see data primitives). I too had trouble early on. I confused myself with the difference between the text I would use in a variable name and the string value assigned to it.

1 Like

This is super helpful. As to point 1, I understand perfectly now: A string isn’t necessarily text—it’s any alpha or numeric or alphanumeric character sequence placed in quotation marks.

As to question 2, I think I understand. You’re saying that because this character sequence (“10011”) has been designated a string (by virtue of the quotation marks), it must be parsed in order to be interpreted/represented as a number. When you say it “has to be interpreted as a number,” do you mean because that’s what this function calls for?

I’m not an expert on the character index computers use, but here are a few links on the subject. Basically computers only understand numbers. We use characters. So we have a sequential index of characters index by numbers and a string is a collection of these characters as a data type we can interact with.

If I had to abstract this I would say a string is kind of like a web domain. We have “www.google.com” rather than typing in the actual IP address of their server.

http://www.pld.ttu.ee/~marek/PA_R4/ascii.html

http://www.asciitable.com/

http://www.unicode.org/standard/WhatIsUnicode.html

1 Like

So a character in a string actually has a numerical value to it. If you compare a capital “A” to a lower case “a” in an if condition, it will return either true or false because the value of “A” is I think higher than “a” because “a” comes first and “A” later in the index.

if( String.charCodeAt("A") > String.charCodeAt("a") ) { console.log("true"); }
else{ console.log("false"); }

console.log(String.charCodAt("A"), String.charCodeAt("a")); // 65, 97
1 Like

Excellent point—thank you. I’ve reviewed data types in the past few days (not thoroughly enough, obviously), but I’m not familiar with data primitives. I’ll look it up.

You could actually write a for loop as well if you wanted to print each character in the String object using the fromCodePoint() method. This grabs a character at a certain index while the charCodeAt() method grabs the index of a character.

for( let i = 0, characters = []; i < 250; i++ ){
  characters.push( String.fromCodePoint(i) );

  if( i === 249 ){ console.table(characters); };
};

edit: sorry, wrong method, fix it now.

1 Like

When I say it “has to be interpreted” I meant only that it has to because that was the task required. My explanation was a bit loose.

To give an explanation that hopefully answers your question and doesn’t get too deep, a function performs a function. It takes some input and gives back an output normally based on the input. The parseInt function takes a string (of numeric characters) and gives back a number parsed from the string (as an integer, hence the name parseInt).

Going a little bit deeper, the browser is what has to interpret the string. You tell the browser, “Hey I want the string “10011” as a numeric value.” in the language known as JavaScript and the browser says, “OK, here is the number you asked for”. You can think of it as asking someone, “Where is the bathroom?” in the language they understand so you can avoid a blank stare as a response.

The internet browser program (IE, Firefox, Chrome, etc) doesn’t see 1 and “1” as the same thing so you have to be sure you are telling the browser what you want in the way it understands.

1 Like

Ah, URL to IP is a good analogy. And thanks for the links. Earlier today I read about four numerical systems—binary, decimal, octal, and hexadecimal (there are many more, but those seem to be the main ones used in this context). The charts at the links you sent me show how the various numeric systems relate to one another. Very useful.

Incidentally, I noticed that the names can help you remember the radix:

  • Bi– means “two,” and the binary system has a base of 2.

  • Deca– means “10,” and the decimal system is based on 10.

  • Oct– means “eight,” and the octal numbering system has a base of 8.

  • Hex– is harder to explain. The prefix hex– means “six.” Yet the base of the hexadecimal numbering system is 16, not 6. If anyone knows how hex– is related to 16, I’d love to hear the backstory.

1 Like

Actually, perhaps the answer is in your post.

10 + 6 = 16 It’s kind of like the roman numeral numbering system.

Yes, fantastic explanation. That’s what I thought you meant: this string has to be interpreted as a number in this case because that’s what the parseInt function does—you give it a string, and it returns a numeric representation of that string. Got it! :white_check_mark:

Oh, that makes sense, since the characters used in the hexadecimal system are 0 through nine plus six letters (A through F).

Thanks! :flashlight: (No lightbulb emoji!)

1 Like