Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

Andrico Karoulla
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrico Karoulla
Front End Web Development Techdegree Student 13,760 Points

Check out my Game of Thrones Themed Quiz! (Spoilers)

/*
    A Song of Fire and Ice Quiz
*/

// Answers for Questions


var answer1 = 'NED STARK';
var answer2 = 'CASTERLY ROCK';
var answer3 = 'BRAAVOS';
var answer4 = 'ROOSE BOLTON';
var answer5 = 'LADY STONEHEART';

// Score Counter
var counter = 0;

// 5 Questions
var question1 = prompt('Who is the father of Robb Stark');
    if (question1.toUpperCase() === answer1) {
    counter += 1;
    alert("Good Job, you got that one right.");
    } else {
    alert("I'm afraid Ned Stark is Robb's father.");
    }

var question2 = prompt('Where is the seat of House Lannister');
    if (question2.toUpperCase() === answer2) {
    counter += 1;
    alert("Nice one, that was correct");
    } else {
    alert("Not quite, the Lannisters are seated at Casterly Rock.");
    }

var question3 = prompt('Where is Arya training to become a Faceless Man?');
    if (question3.toUpperCase() === answer3) {
    counter += 1;
    alert("Awesome, that was the right answer.");
    } else {
    alert("Wrong, Arya is training with Jaqen in Braavos.");
    }

var question4 = prompt('Who fatally stabbed Robb Stark at the Twins?');
    if (question4.toUpperCase() === answer4) {
    counter += 1;
    alert("Sweet, only one more question to go.");
    } else {
    alert("Unfortunately, Roose Bolton dealt the heartbreaking blow.");
    }

var question5 = prompt('Catelyn Tully takes on an alias after the Red Wedding, what is it?');
    if (question5.toUpperCase() === answer5) {
    counter += 1;
    alert("Perfect, that was correct. Let's see your final score.");
    } else {
    alert("After being resurrected, Catelyn travles with the BwB under the name Lady Stoneheart");
    }

// Post-game score analysis

    document.write("<h2>Your final score is " + counter + ".</h2>");

    if (counter === 5) {
    document.write("<p>Nice job, you for the top score!</p>");
    } else if (counter => 3) {
    document.write("<p>Not bad, not quite top score</p>");
    } else if (counter => 1) {
    document.write("<p>Not good, read more!</p>");
    } else {
    document.write("<p>You got none right.</p>");
    }

Any advice on ways I could improve my code?

3 Answers

ahmed suleiman
PLUS
ahmed suleiman
Courses Plus Student 11,685 Points

Nicely done Andrico played your game seems great, quick advice did you do array, loops, and objects, I am sure your code will be a lot simple if you put your answers in an array then use loop to compare your user input to your answers, plus did you think to print out the questions the user got wrong, hint use two dimensional arrays to put your questions and answers

Nice job! You're not too far from the Loops section of JavaScript. I think it would be really cool to revisit this quiz program with your new knowledge of loops when you're ready.

ahmed suleiman
PLUS
ahmed suleiman
Courses Plus Student 11,685 Points

Oh ok bro take care good luck with it I am sure your project is worth revisiting after arrays and loops