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 trialMichael Kristensen
Full Stack JavaScript Techdegree Graduate 26,251 PointsBoolean Tracker or New Array?
In the task, I started considering using a Boolean Array to track which questions had been answered correctly (True for correct answers and False for incorrect ones seemed a very intuitive way to track it, and the array would just correspond to the index position of the Questions/Answers array, so the referencing between them was easy enough).
if (answer.toLowerCase() === response.toLowerCase()) {
rightAnswers++;
answerTracker[i] = true;
} else {
answerTracker[i] = false;
}
I would then later, as necessary, simply pull the corresponding questions and answers from the original Q&A array, when building the List items for the HTML - arrTracker being the answerTracker array, arrData being the Q&A array, and the bool value being whether or not we are writing the list for the correct answers or incorrect answers with the current function call.
function createListItems(arrTracker, arrData, bool) {
let items = '';
for (let i = 0; i < arrTracker.length; i++) {
if (arrTracker[i] == bool) {
items += `<li>${arrData[i][0]} - ${arrData[i][1]}</li>`;
}
}
return items;
}
All in all, I saved having to make 2 arrays (Right Answers and Wrong Answers) to track data already in the Q&A Array, and I now store less complex variables in the array, while still being able to perform a later lookup for both the Answers or Questions, by cross-referencing the two arrays.
- Are there any reasons that I haven't spotted yet, that Guil is using two separate arrays to track the correct or wrong answers?
1 Answer
Steven Parker
231,269 PointsYou'll find that many of the lesson examples are created specifically to illustrate the concept of the lesson, and will not always be the most efficient approach to a specific issue.
Spotting ways of making the example more compact or efficient is just evidence of your learning progress.