Jonas: Hello there.
It's time for another Coding Challenge
so that you can apply some of the concepts
that you just learned on a fun
and practical coding exercise.
So in this challenge, we're going
to build a fun quiz game right here in the console.
So you see here in the console, we have a question
and then in here, we have this pop-up window
where we can insert the correct answer.
So there's no fancier interface.
We do everything just here in the console
and using this prompt window.
So in here we can select a correct answer,
and the question is
"Is JavaScript the coolest programming language
in the world?"
And yes, of course, it is, right?
So let's insert "zero" here.
Okay, and then correct answer.
So if you want to play again,
we just reload the page, we get another question,
and right now I only have three questions,
and these are selected randomly,
so it's very likely that we have the same questions again.
So "zero" again.
Let's reload this
again,
and now we have a new question
"So what's the name of this course's teacher?"
And as you already know, it's number two, Jonas, right?
All right, so you see how this works, right?
So let's take out some instructions for this challenge.
So again, let's build a fun quiz game in the console.
The first step is to build a function constructor
called "Question" to describe a question.
And a question should include, of course,
the question itself, right?
And the answers
from which the player can choose the correct answer,
and in here, you should choose an adequate data structure
like an array, an object, or whatever you think is best.
And then, also we need to store the correct answer
so that we can check if the input answer is correct.
And for this one, I would use a number.
Okay, and then when you have the func and constructor
properly set up, you can then use it
to create a couple of questions.
And then number three, store all of these questions
inside an array because it makes it a lot easier
to code step number four,
which is to select one random question
and log it to the console together
with the possible answers, of course.
And then each question should have a number
such as we saw in the demonstration.
And the hint that I can give you for this is
to write a method for the question objects for this task.
Then, after that, you use the prompt function
to ask the user for the correct answer.
And the user should then input the number
of the correct answer, and these numbers are
of course, the numbers that you displayed in task four.
And then finally, you check if the answer is correct,
and print to the console
whether the answer is correct or not.
And I would write another method for doing this.
Now in the end, suppose that this code would be a plug in
for other programmers to use in their code.
So we have to make sure that all of your code is private
and doesn't interfere with the other programmer's code,
and we learned a special technique
in this section to do exactly that,
so use that one to complete this step.
So this is the most basic version of the quiz,
but this time I also have an expert level.
And so let's take a look at that first,
and then let's see the instructions here.
So this time after we answer to one question,
we will always get a new question.
And we also keep a score and display it
so that we know how many answers were correct.
So again, "What is the name of this course's teacher?"
And number two, Jonas.
You see we get a new question right away here, right?
We see the correct answer, then your score is one
because we have one correct answer at this point,
and then we get the new question here.
So "Is JavaScript the coolest programming
language in the world?"
And yes.
Okay, so now we have two points.
So "What's the name of the course's teacher" again?
So let's now put a wrong answer here.
Let's say one, and then wrong answer, try again,
and our current score keeps at two, such as it was before.
So this question again, so number two, "zero"
and here is the third question that I wanted to show you.
So "What does best describe coding?"
It's boring, hard, fun, or tedious?
So the answer is, of course, number two, right?
And now our current score is five.
So this version of the quiz is a lot more fun, right?
So let's see the instructions for this one.
So after you display the result,
you should display the next random question
so that the game never ends.
And if I were you, I would write a function for this,
and then call it right after displaying the result.
But be careful because if you do that,
then the game will actually literally never end.
So you have to include an option to quit the game
if the user writes "exit" instead of the answer,
and then in this case, you don't call the function here
in task eight.
Let me actually show that to you.
Okay.
Because if I hit cancel,
then I will always get a new window, you see?
I cannot close this, and so the only way
to exit this game is to write "exit" here,
and that's because I coded it this way.
So "exit" and then the game ends.
Okay, so you have to code this too.
Now after that works, you can track the user's score
to make the game even more fun.
So each time that the answer is correct,
you just add one point to the score, and for this task,
I am actually going to use the power of closures,
but of course, you don't have to, you can do it
in another way because for me,
the most important thing at this point,
is that you actually write the code on your own,
so that you really try to solve these problems
on your own, okay?
It's absolutely no problem
if your code is different from mine.
The most important thing again is
that you should really do it on your own
at this point because right now
in the beginning of your journey,
the most important thing is that you really practice,
and that your code works.
Then, optimizing your code to make it perfect,
that comes after that.
But for now, just get your hands dirty with some coding,
and not trying to make it perfect, okay?
Then finally, in the end, just display the score
in a console such as you saw in the demo before.
And for that, you can use another method.
So again, just try to code this on your own,
and then after that, once everything works the way
I showed you, you can check out the solution
to see the way I implemented this game, okay?
So good luck, and take all the time that you need.