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 Arrays Multidimensional Arrays Improve the Quiz – One Solution

Cory Rhodes
Cory Rhodes
5,096 Points

I am getting 'undefined' as well. I decided to the assignment a little differently; but anyway, here is the code.

const quiz = [

[ "How many stooges does it take to screw in a light bulb?", "A: one", "B: two", "C. three" ],

[ "What's the difference between a cult and a church?", "A: a cult allows gays to join, a church doesn't", "B: a church is a cult", "C: people who go to church go to heaven." ],

[ "What is considerd a healthy sex life?", "A: frequently with one partner", "B: once in a while", "C: frequently, but with a hooker" ]

];

const answers = [ 'c', 'a', 'c', ];

// 2. Store the number of questions answered correctly

function getResponses(arr) {

let responseList = [];

for ( i = 0; i < arr.length; i++ ) {

//let response;

    responseList.push(prompt(`*** ONLY write the LETTER that corresponds to the answer, WITHOUT periods, commas, etc. ***

    Question #${i + 1}: ${arr[i][0]}

    ${quiz[i][1]}

    ${quiz[i][2]}

    ${quiz[i][3]}`) );

}

return responseList;

} const responses = getResponses(quiz);

function getScore(arr1, arr2) {

let score = 0;

for ( i = 0; i < answers.length; i++ ) {

    if ( arr2[i].toLowerCase() === arr1[i] ) {

        score++;

    }

}

return score;

}

const score = getScore(answers, responses);

function recordResponses(arr1, arr2) {

let items = [];

for ( i = 0; i < answers.length; i++ ) {

    if ( arr2[i].toLowerCase() === arr1[i] ) {

        items += `<li>Correct, the answer is ${arr1[i].toUpperCase()}, you answered     

${arr2[i].toUpperCase()}</li>`;

    } else {


        items += `<li>Incorrect, you answered ${arr2[i].toUpperCase()} the correct answer was 

${arr1[i].toUpperCase()}</li>`;

    }

}

return items;

}

function congragulate(score) {

let paragraphBuilder;


if ( score === 3 ) {


        paragraphBuilder += ( `<p>You got 100% of the answers correct! Keep up the good work!</p>`);


    } else if ( score === 2 ) {


        paragraphBuilder += ( `<p>Good job! You almost got a perfect score!</p>` );


    } else {


        paragraphBuilder += ( `<p>You did not pass the quiz.  Keep on improving before moving on!</p>` );


    }


return paragraphBuilder;

}

let html = `

<h1>Here are your Quiz Results</h1>


<h2>You got ${score} out of ${answers.length} answers correct.</h2>


${congragulate(score)}


<ol>

    ${recordResponses(answers, responses)}


</ol>

`;

document.querySelector('main').innerHTML = html;

1 Answer

Steven Parker
Steven Parker
231,269 Points

In the function "congragulate", you create a new variable named "paragraphBuilder", then a string (one of 3) is added to this variable using the concatenating assignment operator (+=). Since this operation adds onto a string, and since the variable had not yet been given any contents, the value "undefined" will be displayed in front of the string added in.

Using a normal assignment (=) instead of the special one will fix this.

Steven Parker is right, but ultimately, you don't need the paragraphBuilder variable at all. Since you're just returning a string, you could simplify your function like this:

function congragulate(score) {
    if ( score === 3 ) return `<p>You got 100% of the answers correct! Keep up the good work!</p>`;
    if ( score === 2 ) return `<p>Good job! You almost got a perfect score!</p>`;
    return `<p>You did not pass the quiz.  Keep on improving before moving on!</p>`;
}