Check out a free preview of the full JavaScript: From First Steps to Professional course

The "Operators Exercise" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana provides an exercise to practice using arithmetic operators, including, addition, subtraction, multiplication, and division.

Preview
Close

Transcript from the "Operators Exercise" Lesson

[00:00:00]
>> Okay, so what we're gonna do right now is some math. Yeah, we love math, everybody loves math. [LAUGH] What we're gonna do is just try out some of these arithmetic operators to compute a few things. The total number of siblings of your parents, however, many parents you may have, how many siblings do they have in total?

[00:00:23]
This is a question that only you and JavaScript can answer. How about the average number of hours that you slept this week? So let's say, this week is the last seven days, so you can guess to me. I'm not gonna like no [LAUGH], and then another question is how would you calculate assuming the average number of hours that you slept this week?

[00:00:46]
How would you estimate the number of dogs you'd pet in a week if you could pet 1 dog per hour during your waking hours? So we're just gonna try out some of our arithmetic operators in order to compute these very important facts about our lives, okay? Let's take a moment, try it out, and then we'll talk about it together.

[00:01:15]
Okey-doke, we're back and we've done some math, so let's talk about it. All right, the total number of siblings of your parents. Now, obviously, we're all gonna have different answers to this, but you probably used which operator in computing this answer?
>> Plus sign.
>> Plus, yes, plus version, so here, for example, for me, I have two parents.

[00:01:43]
Well, my mom had one brother and my dad had two siblings. So together, they have three siblings, so I have three aunts and uncles. Let's say, and y'all probably had different things, maybe you have, I don't know, other folks in your family, but the plus operator is what we needed here.

[00:02:05]
Okay, how about the average number of hours you slept this week? Now, there could be a few different operators we might need to use for this. But when we talk about averages, we're usually gonna use at some point, a certain operator to find out the average over a certain amount of time.

[00:02:26]
So for example, if there's 7 days per week, I'm gonna take whatever total hours slept and probably use which operator to get the average over 7 days.
>> Division.
>> Division, exactly, so this slash. Okay, now, this is not a thing, cuz I don't have a total number of hours slept.

[00:02:49]
But maybe I have that in parentheses, the number of hours that I slept each day. Now, this is obviously not correct, because I never get eight hours of sleep a night. But let's assume that I get eight hours of sleep a night, I could do something like 8 + 8 + 8 + etc., seven times.

[00:03:08]
Yeah, it's one way I could do it, or I could maybe do 8 x 7. Now, this isn't a very exciting math problem, right, cuz I'm multiplying by 7 and then dividing by 7. We know that this would come out to be 8, which, again, is a fictional number.

[00:03:24]
But it is not [LAUGH] the correct answer for me anyway. But I hope it's true for you all, cuz sleep is important. Point is we might be using addition, we might be using multiplication. And then eventually to get an average, we're usually gonna use some kind of division, you get the idea.

[00:03:38]
All right, so how about our dog hours? If y'all are dog people like me, if not, maybe you don't wanna pet one dog per hour maybe you wanna pet zero dogs per hour. But in this case, let's talk through it, let's sort of teamwork this. How can we compute the number of dogs I'd pet in a week, seven days, if I pet 1 dog per hour assuming that I sleep eight hours a night?

[00:04:05]
So how can we compute this? So maybe we start by calculating the number of hours per day that I'm awake. If I sleep 8 hours a night, [LAUGH] what operator could I use to figure out the number of hours I'm awake if I sleep 8 hours a night, and there's 24 hours in a day?

[00:04:27]
Could be like-
>> Minus sign.
>> Sorry?
>> Minus sign.
>> 24-8 would give me, okay, so I have 16 dog petting hours per day. And then, I'm gonna pet how many dogs per hour? So that's gonna need which operator? If I have a number of dogs per hour, Like-

[00:04:47]
>> 1?
>> Times, right, so star for multiply, times 1 dog per hour. Maybe I can really up my dog petting ratio and pet a dog every half an hour, or depends on the density of dogs in my neighborhood, etc. But let's say, times 1, and so that is going to be then the number of dogs that I could pet in a day.

[00:05:11]
And then if I wanna get the number of dogs I could pet in a week, I can add, well, sorry, I can extend my math here with times 7. And when we say times, we mean star 7, 112 dogs. Wow, I actually have no idea if that's sounds like a lot of dogs.

[00:05:31]
I would be really happy if I could pet 112 dogs a week. [LAUGH] Okay, but the point is, what we have here is an example of we need to use those parentheses. Because otherwise, if I didn't have these parentheses here, what would happen? What would happen first in JavaScript's brain?

[00:05:53]
>> 8 times 1.
>> 8 times 1 would happen first, so I would get a totally different number. Negative 32 dogs? Unacceptable, unacceptable, I'm not gonna give back dog pets, no. So the point is parentheses, we need them, they're our friends, they show up all the time in JavaScript.

[00:06:09]
Okay, any questions about math? [LAUGH] Please don't make them hard. We had a great question actually in the room which was about some other operators. So folks might have found out, I don't know, maybe I want to exponentially grow my dog petting abilities. And I want to raise a number to a certain exponent or to a certain power, and what is the operator for that?

[00:06:35]
Or maybe I find that there's some other symbol in my keyboard and I don't know what it does. Let's say, for example, the percent symbol, what does that do? I don't know, let's find out. How do we find out? We go to MDN, and if I type in operator, I should get to this.

[00:07:00]
For example, there's this page, a reference of all the expressions and operators in JavaScript. This is a long page, long page, but if I'm curious about a particular operation, I can do Ctrl+F or Cmd+F to find something in the page like exponent, let's say. Maybe if I spell it right, okay, it'll show me, okay, here's this exponentiation operator.

[00:07:24]
And that shows me that, it turns out that if I wanna raise a number to another number to the power of another number, I can use double asterisks. That is a valid operator in JavaScript for exponentiation. And likewise, if I want to find out what is that percentage sign do, I can control or Cmd+F, search in the page, look for the percent sign.

[00:07:48]
I find that it highlights this remainder operator. And then this will tell me, okay, this percent sign, which we sometimes also call modulo returns the remainder leftover when you divide one number by another. So there's a whole bunch more operators out there, you can always look them up in MDN.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now