02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Numbers

Numbers are an essential part of any language including JavaScript.

We have already covered that JavaScript has three data types: Strings, Numbers, and Booleans. It only has one variable to store all three data types; it’s up to you to look at this as a pro or a con. It’s a good idea to make peace with it and treat it as a pro; you know, the glass half-full approach.

Moving back to numbers, we can kill two birds with one stone. First let’s see what PROMPT is in JavaScript. "User input" is when you ask the user for data. How exactly will the data come to us so we can access it in our code and manipulate it? "prompt" is the answer for now(in a real live application if it was 1987 but for now would be the correct answer as well). prompt() is a function which permits us to get data from the user: watch the video carefully and you will see how it provides this interface.

prompt() accepts two string parameters. The first one is the message which will be displayed on the pop-up box like “How Old Are You?” while the second argument is what should be displayed in the text box where the user will input the data. This argument is usually left blank unless you want users to have a certain default string so they can just click OK.

What will "prompt" return to us? Whatever the user enters in the prompt box will be returned as a string. Remember "prompt" only returns a string, even if the user enters 23. "23" will be stored in a a string and cannot be used as a number. Here it is in action:

var userAge = prompt(“Enter your age”, “ ”);

In depth look

Let’s look at this string returning in detail. We have a variable var num1 which has 10 stored in it: we will ask the user for a number which we can add to variable num1. We use "prompt" and ask the user for the second number, a pop up box is displayed with the message “Please Enter a Number”: the user types 10, which we store in another variable called var num2. Now we display the result using "alert" like this:

alert(num1 + num2);

What do you think we will get?

If you answered 20 -- BEEP. Your logic is right but the answer is wrong, JavaScript does not know that num1 and num2 are Numbers, remember? It assumes they are strings and simply concatenates num1 and num2. The alert will give us “1010” rather than 20.

How do we solve this? JavaScript provides us with numerous functions. You just have to learn to choose the right function at the right time. In this case we want to add num1 and num2 -- we use the function parseFloat(); it accepts a string argument and outputs a number. Simple as that.

Now when we call "alert" like this:

alert(parsetFloat(num1) + parseFloat(num2));

We will get the desired result of 20, because parseFloat(); converts our string inputs into what JavaScript now recognizes as numbers: therefore it adds them instead of concatenating the strings.

ParseFloat(); can also convert to decimal values. If we want an integer there is parseInt(); which returns only integer values. We can additionally perform all the basic math functions on variables such as num1/num2, num1*num2, num1-num2. Play around with this and you'll quickly learn it.

It’s a good idea to watch this video more than once as it will help you understand how JavaScript deals with numbers and converts them from strings.

Video Transcript

00:01All right, so we learned about a variable we learned about comments now. Let's look at numbers
00:06Then create a new variable which will be a number and that number is going to be any number, right?
00:12So we're gonna get here number one and we're gonna equal that and we're gonna see something new
00:17Well, let's start playing with one a few of those built-in elements
00:21So we store how the alert works what I want to do next is I want to show you how the prompt works now the prompt
00:28The prompt basically accepts two parameters. My first parameter is going to be
00:34pick a number between one and ten
00:43Our second parameter is going to be empty because I don't want to put anything there
00:46I could have put it but we'll see what happens after I do that and I'm gonna
00:49Just run this for a second then we'll see it again
00:51So first of all, I get a prompt saying to me pick a number between one and ten
00:56Because I'm not a cheater
00:57I'm gonna type in just five because I want to I'm gonna put a number between one and ten and I'm gonna click okay
01:02And there we go. And what did that do? First of all, my first element to pick a number between one and ten
01:07We saw that now if I put here any copy and I ran it again
01:11You would see that the copy would be in the second line
01:13But we really don't want that we want only a number here between so I so that's what we kept it empty
01:19And again, you can notice that this prompt action or function which we'll see what functions really aren't in the future
01:25date as we said
01:27Expects two parameters now so that when we work with the alert
01:30There was one parameter which was the copy that would be displayed in this scenario
01:35What is the parameter is basically information that that function expects in?
01:40Our scenario the prompt function expects two parameters
01:43The first parameter is the copy that's going to be there in our prompt
01:47And the second one is the value in that selection copy that enables the user to put information
01:52We could have put a default value in our scenario
01:55we wanted to make sure that the default value was nothing, but you've noticed we've just sent two strings and
02:01Notice that we're using a comma to separate between the two
02:06elements
02:07Contrary when earlier we put a plus what did that do it combined the two together in our scenario here a comma basically
02:15Separates between the two enables us to send more than one into a function that expects two parameters
02:21All right, so now we're using the prompt and the prompt returns a value and that value that is returned goes inside of num1
02:29Now I want to create another variable
02:31So I'm gonna call it num2 and we're gonna use a prompt again, and it's the exact same thing
02:45So we're not gonna explain it one more time now the last thing
02:48I want to do is I want to let's say I want to output now. I want to give an alert
02:54You picked the numbers we're gonna talk about this more in a second
03:09Alert let's just get it running and see that it works, and we'll talk about it more
03:13So if I my first number and oh let me just cancel this and refresh because now we're in a new screen
03:18So the first number is gonna be five the second one is gonna be one and we see you pick the numbers five and one
03:25beautiful now
03:27Obviously this wasn't very useful in our scenario
03:29and I want you to notice one thing before we start doing mathematics is you'll notice that what I did here is I created a
03:36When when I did my alert I sent a lot of information
03:39But all this information was only one unit. It was only one string. Why is that?
03:45Because when I took this information
03:47I did a plus num1 and num1 was a number right but for our scenario a string plus a number
03:53It's basically saying a string plus a string JavaScript will automatically convert that number into a string
03:58So now when because it doesn't really know it's a number we never defined it
04:02That's what the hard part in JavaScript is you have to notice your data types
04:07So you don't get confused so in our scenario we took all these numbers
04:10But we're treating them as strings and thus it connected all them together using the plus cuz plus when you use plus with a string
04:17You're connecting all the strings together now on the other hand. I want to show you something
04:21I'm gonna create our a new variable. This is gonna be the
04:24sum and
04:26The sum is gonna be num1 plus num2
04:29and what I want to do is I'm just gonna delete all this good stuff put here the sum and
04:36We'll rename this the sum of your numbers is
04:41Gonna save that and run again. Let's see if it works
04:44So I'm gonna put here the number three and two
04:48so the number we hope to get would be five right, but we're getting 32 and
04:52I'm hoping you really understand why that's happening right now. If you don't we're gonna find out immediately
04:57Excellent. So basically we got 32. Let's see why let's go back into our source code
05:02And when we're looking at our source code, although we think we're connecting between two numbers and deliberately I did this
05:11Sometimes we might think it's a number just because we called our variable num1
05:14But actually we really wanted it to be a number and it's not it's a string
05:19Why is it a string because the prompt returns strings? It doesn't return numbers
05:23Now although it returns
05:26Strings and not numbers. We want to fix this issue
05:28And now although we never created functions on our own and we don't really know what functions are but functions enable us
05:35To do some sort of an action now. We've seen functions already a few times
05:40We've seen them with the prompt and the alert and the functions are different than variables in just one way
05:45We put the name of the function and then we open a round bracket
05:49Put some information inside or not and close a round bracket
05:54In our scenario, there's a function that's called parse float and parse int in our scenario
06:01We know that the numbers are going to be between 1 and 10 and they could only be numbers between 1 and 10
06:05So we're not expecting people to put well, they might put a floating number, right? They might put 1.5. Maybe they don't put an
06:13Round number we didn't ask her a round number
06:16So we won't use the parse int which basically would return integers, which basically mean
06:22Rounded numbers, so we're going to use the parse
06:25float
06:27Round that up inside of our number and we're going to use that parse float both for the num1 and num2 basically opening a round
06:34Bracket and closing it and then putting the value that we want to put inside which is a number right now
06:39Which we want it to be a string
06:41Through our parse float now if we run this one more time, hopefully our issue will be solved
06:46So if we put here again 3 and 2 we get the value of 5 which is exactly what we wanted
06:51and if we put here a value, that's maybe 1.3 and
06:552.3, so we hope we'll get here a value that's close to what we wanted and don't worry about the numbers not being accurate
07:02Programming languages are just not accurate when you go down to floating numbers because
07:08They just aren't
07:10Contrary to a calculator which sometimes is better at that programming languages
07:15Notoriously client-side languages are very not accurate
07:18So in our scenario 3.5 9 9 9 9 9 9 9 6 is more or less 3.6, which what we would hope to get
07:25All right
07:26So so we got that covered and we've now seen a little bit more of using functions
07:32Now we've seen how to combine between two numbers that were strings originally
07:36So now that we took something that was that complicated. Let's just make it even a more a little bit more complicated
07:42Alright, so if I take my summary now, I want to take that summary, but I want it to be an average
07:48So I'm gonna call it AVG and I'm gonna take my summary and I'm gonna divide that summary by 2
07:55So as you can see I could use basic
07:57Mathematic operations and what are those up mathematic operations that you put here a comma. So there's obviously the plus there's the minus
08:04There's the times there's a divide those basic mathematical operations
08:09All right. So basically what we're doing here is we're taking that number
08:12we're dividing it by 2 and now we have a new average and we could say that the
08:19AVG of your numbers is AVG and we've just created another variable
08:23It was that easy and if we run our application just one more time really quickly and we put inside our first prompt
08:29Let's say we put the value of 10 the second one we put one
08:32We're gonna see that our average is 5.5, which is exactly what we expect it to be
08:38All right, so we've we were growing right we're growing as developers also. We've seen how to work with strings
08:44We've seen how to work with numbers
08:45We've played around a little bit with functions even though we haven't learned about them yet, but we're using them
08:51So the next thing I want to do is I want to again comment down this whole phase
08:54So let me just put here a comment for
08:57playing
08:58with
08:59Strings, right?
09:00So that was my comment and then I have your opening comment all the way until here closing comment
09:06And we could probably delete that one line common because now we have an example that in the first line
09:11and we could put here a second one and the second one was
09:14playing with numbers and
09:17I'm gonna comment that whole playing with numbers all the way from the beginning until the end
09:22So all this is now commented out. So now nothing will happen. Our code is basically absolutely empty

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!