Basic Math in JavaScript
Now that we are able to use variables and user input, we can really start coding the fun stuff!
We can do basic math!
In our first program, we are going to create a simple calculator. This will allow us to add 2 numbers and print out the sum.
We will read an integer from the user and store it in our first variable, then read another integer from the user and store it in the second variable. Lastly, we will add the two variables together. Let's see what this looks like:
The output looks like:
Arithmetic operators in Javascript
There are five arithmetic operators that we can use with Javascript, most that you have likely used before.
Here are some basic examples:
The basic rules of arithmetic apply here.
What the heck is a modulus?
Remember when you learned long division and you would divide two numbers and have an integer as a remainder? Modulus means divide and take the remainder.
What is 17 % 5?
If we say, "What is 17 divided by 5?" The answer is 3 with 2 left over. Check out the long form of division:
The answer to 17 % 5 is 2.
Increment and Decrement
It is very common in programming to want to add one (increment) or subtract one (decrement) a variable. An example for this is if we want to subtract one (decrement) from a store's inventory whenever an item has been purchased.
Previously, if we wanted to add one to the current value of a variable, counter
, it would look like:
A faster way to do the same logic is to add ++
to a variable, like:
This will add one to counter
just like the previous code.
Similarly, if we want to subtract one, we can add --
like:
There are also other shortcuts available if you want to modify the current value by using arithmetic operations.
Example: Dollars to Pounds
Let's say that we want to write a program that will convert US dollars to British pounds. We will prompt the user for the amount of dollars. Then, we will multiply the conversion rate of dollars to pounds by the amount of dollars that the user specified. Lastly, we will print that value off.
Since we want the conversion rate to stay the same, we will create a variable at the top of our program that is in all capital letters and uses underscores between words. This is called a constant. We can use this constant anywhere in our program, but will not want to change its value.
Example: Dividing Up Groups
This example is a little trickier: We want to divide a large number of people into groups and show how many total people, how many people per group, how many groups, and how many people left over.
This program uses a couple of key concepts:
Math.floor(num1/num2)
will round the result of num1 divided by num2 to an integer. (We don't want half people!)The modulus (%) operator will help us find the remainder of two values.
And that's it! Basic math in programming is both easy and fun.
Last updated
Was this helpful?