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

Maciek Radomski
Python Development Techdegree Student 9,786 PointsMy solution and possible code improvements
Hello,
I would like to ask if this solution is fine and how can I improve my code any help will be appreciated.
// 1. Create a multidimensional array to hold quiz questions and answers
const quiz = [
['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
function getUserAnswers(question) {
const userQuestion = parseInt(prompt(question));
if ( userQuestion ) {
return userQuestion;
} else {
throw new Error('Incorrect input\nincorrect Inputs: "One", "Two", "dsadas", "41a2"\ncorrect Inputs: 1, 2, 3, 4, 5, etc...');
}
}
const userAnswers = [];
for (let i = 0; i < quiz.length; i++) {
userAnswers.push(getUserAnswers(quiz[i][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
*/
function collectAllAnswers(arr) {
const collectedAnswers = [];
for (let i = 0; i < arr.length; i++) {
const checkAnswer = quiz[i].includes(arr[i]);
collectedAnswers.push(checkAnswer);
}
return collectedAnswers;
}
function countPoints(arr) {
let pointsGained = 0;
for (let i = 0; i < arr.length; i++) {
if ( arr[i] ) {
pointsGained += 1;
} else {
console.log(`Bummer, question #${i + 1} was incorrect!`);
}
}
return pointsGained;
}
const pointsGained = countPoints(collectAllAnswers(userAnswers));
// 4. Display the number of correct answers to the user
function printMessageToHTML(message) {
const selector = document.querySelector('main');
return selector.innerHTML = message;
}
const correctQuestionAnswered = collectAllAnswers(userAnswers).filter((arr) => arr === true );
printMessageToHTML(`<h1>TYou got <strong>${pointsGained}</strong> points.</br>
and you got <strong>${correctQuestionAnswered.length}</strong> correct question(s).</h1>`);
`
1 Answer

Steven Parker
242,770 PointsWhen evaluating your code, try checking how it performs in different situations. Given what this program does, you might try running it three times with different inputs for each try. I would suggest:
- only correct inputs
- only incorrect inputs
- a mix of correct and incorrect inputs
If it operates according to the instructions in every case, you can consider your solution valid.
For now, proper operation is the most important criterion. As you progress in the courses, you will learn additional techniques that will allow you to make code improvements in efficiency and/or compactness.