Bummer! The track "iOS Development" has been retired.

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 Question challenge. How's my code?

Github links:

Code link: https://github.com/Torgian/question-program

Running program : http://torgian.github.io/question-program

What do you think? I don't know much about arrays, but I'm pretty sure i used it correctly in this case ( var correctAnswers = [] )

Any suggestions on improvements?

1 Answer

Hi Nathaniel,

Your code is good but correctAnswers should be an integer instead of an array as you're using it as a counter rather than storing values within it, aside from that the rest of your code looks good.

var correctAnswers = 0;

+1 Chris.

Nathaniel Nasarow, You can make this code much DRYer, though, and add in support for unlimited questions/answers without taking up a huge amount of space. This may be a little bit beyond your knowledge for right now, but I added in comments that explain everything I did. Notice that I also added in a couple different languages for saying "yes" for the 2nd question that asks "Is Hokkaido in north Japan?", starting with the Japanese formal "hai". You could add in any amount of language converted "yes"s to your question without having to write tons of || logic. And the same goes for your questions.

var quiz = [
['What region is Osaka a part of?', 'KANSAI', 'KANSAI REGION'],
['Is Hokkaido in north Japan? Yes or No?', 'HAI', 'YES', 'Y', 'SI'],
['What is the OR code in JavaScript?', '||'],
['What is the AND code in JavaScript?', '&&'],
['What city is the district of Sakai in?', 'OSAKA', 'OSAKA CITY']
];
var correctAnswers = 0,
    question, 
    answer, 
    response;

alert('Good evening! I will ask you a series of questions.');

//Iterates over the entire quiz array (starts at 0, ends with the last index of the array)
for (var i = 0; i < quiz.length; i++) {
    //question will be the very first index of each array
    question = quiz[i][0];
    //prompt user with question and convert to an upper case format
    response = prompt(question).toUpperCase();
    //set a new variable that will keep track of whether an answer was found
    //so that multiple answers can be added to each question
    var answerFound = false;
    //start at index 1 which is where first answer resides and iterates over each individual question/answer array
   //this is the loop that goes over every answer stored in the quiz array
    for (var m = 1; m < quiz[i].length; m++) {
        //set answer to be the current index at location m
        answer = quiz[i][m];
        //check to see if user response equals answer at this index
        if (response === answer) {
            alert('That is correct!');
            correctAnswers++;
            //signal that answer has been found
            answerFound = true;
        }
        //if answer was found, break out of the "m" loop
        if (answerFound) {
            break;
        }
    }
    //if after execution of "m" loop, an answer wasn't found
    //that means the answer was incorrect
    if (!answerFound) {
        alert('Sorry, that is not correct!');
    } 
}

//And here's how you can dynamically handle the crown system
//it is the same principle as the whole numbers applied to the ratio
//of correctAnswers to the total questions which is = quiz.length
var ratio = (correctAnswers / quiz.length);


//think of the ratio ranges as the same kind you use in school for getting grades
//you can modify these ratios to be whatever range you wish
//ratio of 1 = 100%
if (ratio === 1) {
    alert('Congratulations! You have answered all 5 questions correctly!');
    alert('You win the gold crown!');
}
//80% to 60%, same as 3 or 4 in your case
else if (ratio <= .8 && ratio >= .6) {
    alert('Good job! You have answered ' + correctAnswers + ' correctly! You win a silver crown!');
}
//less than 60% to 20%, same as 1 or 2 in your case
//this range is a bit broad but you can play with it
else if (ratio < .6 && ratio >= .2) {
    alert('Not bad. You answered ' + correctAnswers + ' correctly. Here\'s a bronze crown. Study and try this again!');
}
//For when they answer less than 20% correctly, or 0 in your case.  
else {
    alert("You answered " + correctAnswers + " correctly.  It wasn't enough to pass the test.  Study hard and come back!");
}

Marcus Parsons, Thanks for your answer. This is beyond what I know already, but I recognize the array format. I will study your answer.

Absolutely. That's why I didn't want to post it as an answer because I assumed it was a bit beyond your experience for right now. I added tons of comments though. I also redid the bottom else-ifs after posting to be a more dynamic range and added comments there. It's one of those things that once you start using for loops more, you'll see exactly what I did.

Oops, I also noticed I had used a variable in my comment that wasn't declared earlier in the comment (but was on my version of the code). I revised it again :)