Introduction to JavaScript Introduction to JavaScript

Quiz Exercise Solution with Numbers

Check out a free preview of the full Introduction to JavaScript course:
The "Quiz Exercise Solution with Numbers" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian live codes the solution to the exercise involving mathematical operations.

Get Unlimited Access Now

Transcript from the "Quiz Exercise Solution with Numbers" Lesson

[00:00:00]
>> Brian Holt: So if (isCorrect),
>> Brian Holt: points += 200, return isCorrect.
>> Brian Holt: squared, I kinda did this one for you. Key here is I used ==. Again, here you could've said ===, but you would have to say parseInt out of this.
>> Brian Holt: That would've worked as well. So that's up to you, but you need to get userAnswer to be a number so you can compare them against each other.

[00:00:45]
>> Brian Holt: If (isCorrect), points += 200, return isCorrect, okay?
>> Brian Holt: Then again parseInt just takes a string and attempts to convert it into a number.
>> Brian Holt: So you're gonna have to do that for most of these.
>> Brian Holt: So, in this one you're gonna say, const isCorrect= userAnswer. === or ==.

[00:01:33]
Either way will do ==here. num1 * num 2.
>> Brian Holt: If isCorrect.
>> Brian Holt: Points += 300, return isCorrect.
>> Brian Holt: Make sense so far?
>> Brian Holt: Okay, age.
>> Brian Holt: So here you're gonna say const isCorrect = currentYear- birthYear == userAnswer.
>> Brian Holt: As you can see here, you can put userAnswer on either side, right it's doesn't matter if it comes before after the equal signs.

[00:02:42]
>> Brian Holt: If (isCorrect) points + = 300 return isCorrect.
>> Brian Holt: Okay. So which of these two numbers is larger?
>> Brian Holt: A couple ways you could have done this if you want to be more explicit about this, which you probably should be. So I num1 is greater than num2, and up here we’ll say let isCorrect so it doesn’t go out of scope.

[00:03:29]
>> Brian Holt: IsCorrect is assigned userAnswer == num1,
>> Brian Holt: else here and then we'll do. isCorrect = userAnswer == num2. Then here will do the points if(isCorrect) points += 300 return is correct. Yes.
>> Speaker 2: Two questions on this. Would this actually work if they're equal? Or I get that the else statement will come to us like if either num2 is greater than num1 or they're equal than else will be evaluated.

[00:04:33]
But if the two numbers are equal, how does it know that the person is putting in num2 as the answer instead of num1?
>> Brian Holt: Okay, that's a good question. So let's say they're both five, right? So this is 5 and this is 5, it's gonna say is 5 greater than 5.

[00:04:50]
That's not right. So then, here, it's going to ask, is the user answer equal to 5?
>> Speaker 2: Okay.
>> Brian Holt: In other words, a number's a number, it doesn't matter which number is equal to 5, they're both 5, right? Make sense?
>> Speaker 2: Yes.
>> Brian Holt: Cool.
>> Speaker 2: I have one more question.

[00:05:05]
>> Brian Holt: Sure.
>> Speaker 2: What is the purpose of returning isCorrect in every function?
>> Brian Holt: So, the code down, like down here. What it is doing. I can go down here. It's calling your function here, which is gonna be this validate function, right? And so it's passing in input into your function and then it's expecting back whether or not the user got the answer correct right?

[00:05:34]
And so you have to give it back to the program of either true or false they got it right. And that's how the program knows that something was right or wrong. Does that make sense?
>> Speaker 2: Yes.
>> Brian Holt: Cool, good question.
>> Speaker 3: So on 999 I put let instead of constant.

[00:05:50]
Sorry I put constant instead of let, and it didn't work. So I was wondering why if I didn't assign a value to constant it wouldn't in 1999 it would only work with let.
>> Brian Holt: That's a good question. With const you have to assign it a value up front.

[00:06:14]
So even if I try and change it right now you can see down here it's actually giving me an error as like hey. You're not assigning const right? Whenever you do a const you have to assign it right away. So here I would have to say something like = 5 right?

[00:06:28]
Then it's gonna be happy. So even though this doesn't look like you're reassigning it you actually kind of are actually literally are. [LAUGH] Answer your question?
>> Speaker 3: Yes.
>> Brian Holt: Cool.
>> Speaker 4: Of your solution and of using math.Max, which would be preferable?
>> Brian Holt: Either, those are both totally valid.

[00:06:54]
>> Speaker 4: Okay.
>> Brian Holt: Yeah, that would be a good way of doing it. It would be something like this, right. const isCorrect = userAnswer ===or ==. Math.max num1, num2. That would be just fine.
>> Speaker 5: Equation about this code.
>> Brian Holt: Sure.
>> Speaker 5: If variable is outside a code block like if a function whatever it's global, right.

[00:07:27]
And it can be called from anywhere. It doesn't matter where it is at the bottom.
>> Brian Holt: It has to be declared at that point, right? So,
>> Brian Holt: Let's say I had some, or let's just put it above points and I tried to call function here that had points.

[00:07:55]
>> Brian Holt: And then I try to call thing right here.
>> Brian Holt: Points isn't defined by the time this is called, right? So it matters when that function's invoked. Does that answer your question?
>> Speaker 5: Yeah.
>> Brian Holt: Cool.
>> Brian Holt: Good questions, any other questions?
>> Brian Holt: Great, I take it that that was one of the harder ones.

[00:08:20]
[LAUGH] No? Maybe not? Easy? Cool, and here we're just gonna return points. Run get score.
>> Brian Holt: Okay, and now hopefully this works.
>> Brian Holt: Capital is Canbera. Correct, Ottawa. Part. HOR, Oops, approach should have made that lowercase, but 422 times 6, 132, right?
>> Brian Holt: Yes, someone's born in 78, that'll be 40 is killing it.

[00:09:21]
I'm pretty sure 21 is larger. And I got 1500 points on my own quiz I wrote [LAUGH].