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 trialChristian Dianne Oro
2,366 PointsUncaught Syntax Error: Invalid Destructuring assignment target?? Can anyone point me to my mistake pls?
Can anyone tell me where I went wrong?I am getting Uncaught SyntaxError: Invalid destructuring assignment target on the console and nothing runs. Appreciate help!
// 1. Create a multidimensional array to hold quiz questions and answers
const questions = [
['How many planets are in the Solar System?', '8'],
['How many continents are there?', '7'],
['How many legs does an insect have?', '6'],
['What year was JavaScript created?', '1995']
];
// 2. Store the number of questions answered correctly
const correct= [];
const incorrect =[];
let answerCorrect= 0;
/*
3. 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 question = questions[i],[0];
let answer = questions[i],[1];
let response = prompt(question);
if (response === answer){
answerCorrect ++;
correct.push(question);
}else {
incorrect.push(question);
}
}
function createListItems(arr) {
let items = '';
for (let i = 0; i < arr.length; i++) {
items += `<li>${arr[i]}</li>`;
}
return items;
}
// 4. Display the number of correct answers to the user
let html= `You have correctly answered ${answerCorrect} questions
<h1> Correct Answers</h1>
<ol> ${createListItems(correct)}</ol>
<h1>You got these questions wrong</h1>
<ol>${createListItems(incorrect)} </ol>
`;
document.querySelector('main').innerHTML = html;
1 Answer
Gergely Bocz
14,244 PointsHi Christian Dianne Oro !
You are referring to the elements of the multidimensional array 'questions' incorrectly.
In your for loop:
for (let i=0; i<questions.length; i++){
let question = questions[i],[0];
let answer = questions[i],[1];
let response = prompt(question);
You should write:
let question = questions[i][0];
let answer = questions[i][1];
Instead of
let question = questions[i],[0];
let answer = questions[i],[1];
So there is no comma between the brackets. If there was a comma between the brackets, then your code would be interpreted like a destructuring assignment, like so:
Make the element at index 0 of the question variable equal to the element at index i of the questions variable and the element at index 1 of the question variable equal to the element at index 0 of the questions variable.
This fails, because question is not an array, so the target of the destructuring assignment is invalid. If it were an array, then question[0] would be equal to questions[i] and question[1] would be equal to questions[0].
If you are interested in destructuring, I suggest the MDN Page