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 trialDaniel Rha
Front End Web Development Techdegree Student 7,565 PointsCan't see what's wrong with my code.
const quiz = [ ["What is this dog's name?", "Kevin"], ["What is your great great grandma?", "Nanny"], ["List the first three numbers", "123"], ["List the three numbers in reverse", "321"], ];
const rightAnswers = [
];
const wrongAnswers = [
]; let correctAnswers = 0;
for (let i=0; i < quiz.length; i++) { let question = quiz[i][0]; let answer = quiz[i][1]; let response = prompt(question);
if ( response === answer) { rightAnswers.push(question);}
else if (response !== answer) {
wrongAnswers.push(question);
}
function createListItems( arr) {
let items = '';
for ( let i = 0; i < arr.length; i++) {
items += <li>${arr[i]}</li>
;
} return items;
}
let html=
<h2>These you got right!</h2>
<ol>${ createListItems(rightAnswers) }</ol>
<h2>These wrong!</h2>
<ol>${ createListItems(wrongAnswers) }</ol>
;
document.querySelector('main').innerHTMl = html;
please help, I think im going to go insane soon
1 Answer
Juan Luna Ramirez
9,038 PointsHere are a few things I see:
- The for loop is missing the closing bracket as mentioned by Brandon in the comments.
- The html you are putting together to then insert into the page has to be a string in your JavaScript code.
- "innerHTMl" should be "innerHTML" with capital "L". Variables and function names are case sensitive.
Check the comments below.
const quiz = [
["What is this dog's name?", "Kevin"],
["What is your great great grandma?", "Nanny"],
["List the first three numbers", "123"],
["List the three numbers in reverse", "321"],
];
const rightAnswers = [];
const wrongAnswers = [];
let correctAnswers = 0;
for (let i = 0; i < quiz.length; i++) {
let question = quiz[i][0];
let answer = quiz[i][1];
let response = prompt(question);
if (response === answer) {
rightAnswers.push(question);
} else if (response !== answer) {
wrongAnswers.push(question);
}
} // 1. missing this closing bracket here
function createListItems(arr) {
let items = '';
for (let i = 0; i < arr.length; i++) {
items += `<li>${arr[i]}</li>`; // 2. html has to be string
}
return items;
}
// 2. again, html has to be a string
let html= `
<h2>These you got right!</h2>
<ol>
${createListItems(rightAnswers)}
</ol>
<h2>These wrong!</h2>
<ol>
${createListItems(wrongAnswers)}
</ol>`;
// 3. you have to use innerHTML with with a capital "L"
document.querySelector('main').innerHTML = html;
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsGood catch on the lowercase “l”, as far as the strings though, Daniel seems to be doing that part correct. Because of the markdown capabilities of the forum, the back tics Daniel is using to create the template literals were interpreted as markdown characters to give the content between them a “code-like” feel. That’s why they don’t show up in Daniel's code, but they are there.
If you were to click on ‘edit question’ for the original question, you’ll see them.
Juan Luna Ramirez
9,038 PointsAh, correct! Good eye reading the code in the question. I actually just copied and pasted into a prettifier which didn't include the back ticks.
Daniel Rha
Front End Web Development Techdegree Student 7,565 PointsThanks for the fixes! I think perhaps the closing for loop was what was causing the problem, it works now
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsHi Daniel,
Aside from not seeing a closing brace for your initial for loop, I'm not seeing anything that jumps out as wrong with your code. What problem are you having? When you run your script, what happens?