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 trialterry okey
3,187 Pointsfunction junction?
This is correct code and it works fine. But I just can't understand how the "createListItems" function is working. I understand how it loops through the array, but I just can't understand what the fourth line in the function is doing? Is the 'i' in the for loop and the 'i' in the function referencing each other, or are the separated because of their scope? Is the first line of the function creating a variable named "arr"?
// 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 correctAnswers = 0;
/*
- 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 ) { correctAnswers ++; 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 =
<h1>You got ${correctAnswers} question(s) correct</h1>
<h2>You got these questions right:</h2>
<ol>${ createListItems(correct) }</ol>
<h2>You got these questions wrong:</h2>
<ol>${createListItems(incorrect) }</ol>
;
document.querySelector('main').innerHTML = html;
terry okey
3,187 Pointsterry okey
3,187 PointsSo when I posted the code it took all the line breaks out. When I say the fourth line of the function, I mean the part that says: for (let i = 0; i < arr.length; i++) and the line: items +=
<li>${arr[i]}</li>
;