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

The "Quiz Project isCorrect Solution" 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 walks through the solution to TODO number five of declaring an isCorrect(guess) function that compares the given argument string to the previously defined fact's answer string.

Preview
Close

Transcript from the "Quiz Project isCorrect Solution" Lesson

[00:00:00]
>> The next thing we wanted to do was declare a function called isCorrect, which more generally is gonna take in a guess string and then it's going to compare that to the answer that you had in your fact object. So let us take a look at that one.

[00:00:20]
OJ, do you wanna walk me through what you did for this one?
>> Sure, I have const isCorrect = A.
>> Equals, sorry.
>> I just have an answer, I just have a value.
>> Okay so isCorrect and then maybe like the correct answer, like maybe-
>> It probably it should be something you're feeding in from the-

[00:00:44]
>> Yeah, so what we wanna be able to do is use this function to pass in a guess that somebody made, which we'll get later. But what I wanna be able to do is call, like this, I wanna be able to call the function on a guess, let's say, so you had a fact where the answer is true.

[00:01:06]
I want it so that if I say isCorrect(false), then this whole thing should return false, because your answer in your fact was true. So this is basically like we're comparing a given guess to the correct answer. So let's work it out together. So let's make this instead of a literal value, let's make it a function.

[00:01:40]
And maybe just for sake of variety, let's use the function keyword instead of our arrows. So let's walk through how we would do that. We could start with the function keyword, then I wanna name the function isCorrect. So I'm gonna type isCorrect. Now I want the parameters to be defined.

[00:02:00]
How do I define what the parameters of this function are gonna be? I see hands going like this. Params and we can give our guests a name. I might call it guess, you could call it guessString. You could call it guessValue. You could call it maybeWrongThing, whatever you want.

[00:02:19]
[LAUGH] Why don't we call it just guess for shortness. And then how do I do the body of the function?
>> Curly braces.
>> Curly braces, beautiful. And in this function, I do wanna return a value. So, I want to return the truth value of whether or not the guess matches the answer in my fact.

[00:02:44]
>> So it's a guess, === fact.answer.TextContent.
>> Fact, so what is fact.answer here?
>> That's the answer to the question that I created above.
>> So fact up here is this object, and so fact.answer is what kind of thing?
>> A boolean.
>> Right, so text content is a property that exists on elements in our DOM tree, booleans themselves, that is the whole value.

[00:03:13]
So it's not gonna have text content. So we want to just actually compare it to the answer, the fact.answer. So we're actually almost done. There's one thing missing.
>> Return.
>> Return. Okay, so now we're gonna take in the guess, and it's going to compare it to fact.answer.

[00:03:34]
We can test this out. Let's go back, reload our page to get our updated code, and I should now have an isCorrect function. Great, I have a function called that and it takes in a guess parameter. And now if I say isCorrect, and my guess is true, then that's right cuz the answer to this question, Arrays are like objects, is true.

[00:04:00]
Yeah, but if I were to call it on false, that's wrong. Okay, so we're gonna use this to judge the person's answer later once we figure out how to capture their answer once they click on a button. We're gonna use our isCorrect function to decide whether they were correct or not.

[00:04:20]
And then we're gonna do what we did in our earlier pop quiz, we're gonna color the button based on whether they were correct or not. So if I were to say true to this, that comes out with the red color to show that it's incorrect, cool? So later we're gonna use this function in our project.

[00:04:46]
All right, any questions at this point about functions? We've done a lot. We've covered a lot of ground. We've talked about functions. We've talked about how they have parameters, they have arguments, they have return values. We've talked about how to declare them with the function keyword. We've talked about how to declare them with fat arrows.

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