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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Marcel Carey
Marcel Carey
2,890 Points

Not getting the Wrong and right answers

let questions = [ ['How many Stars on the flag', 50], ['How many championships has the lakers won?', 10], ['How many terms can a president serve', 2] ] let answers = 0 let question let guess let html = '' let correctAnswers

function print(messages){ document.write(messages)

}

function trivia() { for(let i = 0; i < questions.length; i ++) { question = questions[i][0] answers = questions[i][1] guess = prompt(questions[i][0]) if (guess === answers){ correctAnswers += 1 } else if (guess !== answers) { correctAnswers == 0 } } }

trivia() html = You got this ${answers} correct alert(html)

Mark Casavantes
Mark Casavantes
10,619 Points

You are missing ;]] after correctAnswers. There is an error or errors on your function trivia() line, but I cannot find it. Sorry, I am of limited help.

1 Answer

Steven Parker
Steven Parker
231,236 Points

For future questions, use Markdown formatting to preserve the appearance of the code, or share the entire workspace by making a snapshot and posting the link to it.

But assuming no errors are being concealed by the lack of formatting, this code seems to run properly. But some things I noticed:

  • a "print" function is defined to output to the page but it is never used
  • only the correct count is shown in an alert message
  • there is no code here that would identify which specific questions were right (or wrong)
  • the "else if" could be a plain "else" since it only tests for the opposite of the "if"
  • the code in the "else if" body is doing a comparison instead of an assignment
  • replacing the comparison with an assignment is probably not desirable as it would zero out the score
Marcel Carey
Marcel Carey
2,890 Points
let questions = [
    ['What does the F in FBI stand for?', 'federal'],
    ['In which country is a K2 present?', 'pakistan'],
    ['What NFL Quarterback has been to the most superbowls?', 'tom brady'],
    ['What is the name of the bear in Jungle Book?', 'baloo'],
    ['On a farm a what animal is called a kid ?', 'goat']
]
// I declare some variables
let userGuess 
let wrongAnswer = 0
let rightAnswer = 0
let question
let html  
let answers
// Created a function 
function print(message) {
    document.write(message)
}
// I created a for loop to iterate through the array
    for (let i = 0; i < questions.length; i += 1) {
        question = questions[i][0]
        answers = questions[i][1]
        userGuess = prompt(questions[i][0])
        if (userGuess.toLowerCase() === answers) {
            rightAnswer += 1
        } else {
            wrongAnswer += 1
        }


    }

    if(rightAnswer > 2) {
        alert('Good stuff bro ')
    } else {
        alert('You need more practice')
    }


html = `You got this many ${rightAnswer} right, and you got this many ${wrongAnswer} wrong. `
print(html)

So I went back and fixed some things, if there are any more suggestions I would love to hear them so that I can improve

Steven Parker
Steven Parker
231,236 Points

Looking good now! :+1:   (But you could remove the first level of indenting)