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

Is this javascript implentation okay?

I created this code for the class of Javascript Arrays - Improve-the-quiz-one-solution Is this ok?

// 1. Create a multidimensional array to hold quiz questions and answers const questions = [ ['How many continents are there?', 7], ['What year was Javascript created?', 1995], ['How old are you?', 22] ] const correctArray = [] const wrongArray = [] // 2. Store the number of questions answered correctly let correctlyAnswer=0;

/*

  1. Use a loop to cycle through each question
    • Present each question to the user
    • Compare the user's response to answer in the array
    • If the response matches the answer, the number of correctly answered questions increments by 1
      */ for(let i=0;i<questions.length; i++){ let answer = prompt(`${questions[i][0]}`); if(answer== questions[i][1]){ correctlyAnswer++; correctArray.push(questions[i]); } else{ wrongArray.push(questions[i]); } } // 4. Display the number of correct answers to the user const main = document.querySelector('main') main.innerHTML = `<p> You have answered ${correctlyAnswer} questions</p>`

function testFunction (arr,status){ let response = <h2>You answered ${status} these questions:</h2> for(let i=0; i<arr.length; i++){ response += `<li>${arr[i]}</li>` } return response } main.innerHTML += testFunction(correctArray,'correctly') main.innerHTML += testFunction(wrongArray, 'wrongly' )

1 Answer

Steven Parker
Steven Parker
231,172 Points

This code is probably fine, but it's hard to read here. When posting code, always use Markdown formatting to preserve the code's appearance and retain special symbols.

An even better way to share code is to make a snapshot of your workspace and post the link to it.