Introduction to JavaScript Introduction to JavaScript

Quiz Exercise Solution with Strings

Check out a free preview of the full Introduction to JavaScript course:
The "Quiz Exercise Solution with Strings" 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 string manipulation.

Get Unlimited Access Now

Transcript from the "Quiz Exercise Solution with Strings" Lesson

[00:00:00]
>> Brian Holt: You all should have at least, hopefully, attempted the exercise. And today we're gonna go over the answers.
>> Brian Holt: Just before we get into all these sort of things, again, this is hard. For many of you, this was your very first exposure to programming, and that's tough, right?

[00:00:23]
If you feel like it's hard or if you have people telling you they can't do it, screw them right? You can definitely do this. This is just a matter of persistence, right? I remember I tried multiple times to learn a foreign language, and it took me like my third or fourth time before I finally found a language that I could figure out.

[00:00:40]
[LAUGH] So you can definitely do this, let's just do this together.
>> Brian Holt: The person that's persistent is usually the one that figures all this stuff out. So let's go do this exercise together.
>> Brian Holt: All right, cool. So here we have the first one, Australia. Which hopefully was a good example for you.

[00:01:09]
I'm not doing anything being case, actually, I'm being case sensitive here. If you wanted to go the extra mile, you could have said here, toUpperCase === 'CANBERRA'. Or, yeah, that would have been fine, too. And then here, I'm adding 100 to the point total, right? So only if they got it correct.

[00:01:33]
I think something I failed to mention previously is that as soon as you call return, the function finishes. It doesn't do anything beyond that. So if I come up here and say, return isCorrect, right here, none of that stuff underneath is gonna get executed. So that's something to keep in mind as well.

[00:01:55]
>> Speaker 2: Just inside the curly brackets?
>> Brian Holt: As soon as it runs into a return statement, the function is finished. So that's both useful if you are trying to finish it early and not useful if you're not.
>> Brian Holt: Okay, so let's go do, what is the capital of Canada?

[00:02:17]
If you said Toronto, you are incorrect. So, const isCorrect = UserAnswer === 'OTTAWA'.
>> Brian Holt: if (isCorrect),
>> Brian Holt: points += 100;, return isCorrect.
>> Brian Holt: Okay, that should be enough for that one.
>> Speaker 3: Don't you need to do toUpperCase?
>> Brian Holt: You can. Yeah, in this case though, you'd have to, cuz I did that.

[00:02:59]
>> Brian Holt: Yep.
>> Brian Holt: In all honestly, I don't actually care if you do this part. It's probably useful, because if someone types Ottawa with lowercase, though, it's gonna be frustrating on your quiz. But as long as you're getting the programming principles here, I'm happy.
>> Speaker 4: Is there a best practice for this if you're working with user answers and forums?

[00:03:21]
>> Brian Holt: I think you gotta take that one on a case-by-case basis. Some things you would expect to be insensitive, right? But it's probably pretty, like emails, right? Emails are case insensitive. But something like maybe first names, right, probably are case sensitive. So it depends. It's good question.
>> Brian Holt: Okay, function uppercase.

[00:03:46]
I do care in this case. [LAUGH] So, const isCorrect = userAnswer.toUpperCase, or rather that's their answer. So you wanna check if word is to.UpperCase. UpperCase, right, cuz this is what you're getting back from the user. You expect it to be uppercase, otherwise, they would have gotten the question wrong, right?

[00:04:12]
>> Brian Holt: Then, if(isCorrect),
>> Brian Holt: points += 200. And then you're gonna return isCorrect,
>> Brian Holt: Okay?
>> Brian Holt: First three letters.
>> Brian Holt: const isCorrect =,
>> Brian Holt: userAnswer === word. And there's couple of ways you could have done this. The way I chose to do was with substr from zero, and you go, length 3.

[00:04:58]
But you could have used the other one, which would have been, I think, substring 0. Actually, it probably was the same thing.
>> Brian Holt: Let's check.
>> Brian Holt: Actually, I think it would be 0 to 2,
>> Brian Holt: So,
>> Brian Holt: I'll try that. This is cool.
>> Brian Holt: So there's another one called substring, like this.

[00:05:30]
And if you did 0 to 2, I think that would give you the same thing. Nope, it is 0, 3. So in this case, it doesn't matter. You could have used substr or substring, and the parameters would have been exactly the same. Just to explain to you why that's different,

[00:05:54]
>> Brian Holt: This one, substr, which I had looked this up, by the way, before coming here. So I do not have this memorized. This one starts at index whatever, and then this goes for 3 length, right? So starts at index 0 and goes for 3. This one starts at index 0 and ends at index 3.

[00:06:13]
And when I say index, 0, 1, 2, 3, and so on, right? So-
>> Speaker 4: Say that again, is there a difference? They both start at whatever index and end at index 3, and they're both exclusive. Meaning that it doesn't actually-
>> Brian Holt: Okay, yeah, I see what you're saying.

[00:06:36]
So substring, if I change this to 3, okay? This would start on index 3 and go for length 3. So I have this and i, right there.
>> Brian Holt: Whereas if I did substring and I did the same parameters, it would give me one character I think, one or zero characters.

[00:06:56]
Yeah, it gives me zero characters, cuz it starts at index 3 and ends at index 3.
>> Speaker 4: Okay.
>> Brian Holt: That make sense?
>> Speaker 4: Yes.
>> Brian Holt: I think substring is the preferred way, but honestly, you can use either one. I've been using this one because that's the one I can remember.

[00:07:10]
[LAUGH] So either way.
>> Brian Holt: You'll find there's very rarely one answer to these questions. There's many ways to do this. And often, not one of them is more correct than the other.