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

JavaScript

What's wrong with my code?

var questions = [ ['What color is peluche?', 'white'], ['What is peluche\s last name?', 'miados'], ['What color is peluche\s nose?', 'black'] ];

var correctAnswers = 0; var question; var answer; var response; var html; var correct = []; var wrong = [];

function print(message) { var outputDiv = document.getElementById("output"); outputDiv.innerHTML = message;

}

function buildList(arr) { var listHTML = '<ol>'; for(var i = 0; i < arr.length; i += 1) { listHTML += '<li>' + arr[i] + '</li>'; } listHTML += '</ol>'; return listHTML; }

for (var i = 0; i < questions.length; i += 1) { question = questions[i][0]; answer = questions[i][1]; response = (prompt(question)); if(response.toLowerCase() === answer) { correctAnswers += 1; correct.push(question); } else { wrong.push(question); } }

html = " You got " + correctAnswers + " question(s) right."; html += '<h2>You got these questions correct:</h2>'; html += buildList(correct); html += '<h2>You got the questions wrong;</h2>'; html += buildList(wrong); print(html);

Steven Parker
Steven Parker
229,644 Points

What kind of problem are you having? At first glance it seems like it should work OK.

the html doesn't appear

Steven Parker
Steven Parker
229,644 Points

We might need to see the whole project (only the JavaScript portion is shown here). To share the entire environment, make a snapshot of your workspace and post the link to it here.

this is the html code

<!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> <script src="arraytest.js"></script> </head>

<body> <title>Test</title> <h1>just a test</h1> <div id="output"></div> </body> </html>

1 Answer

Steven Parker
Steven Parker
229,644 Points

I was able to run the code, but it worked just as expected. After answering the 3 pop-up questions, the results appeared on the page.

The problem may be something subtle in the environment. To share the entire environment, make a snapshot of your workspace and post the link to it here.

https://w.trhou.se/f9abcny2mw is that what you need? by the way thank you so much

Steven Parker
Steven Parker
229,644 Points

Yes, the snapshot allows for a complete analysis.

The HTML is attempting to load a script named "arraytest.js", but the name given to the file is "scripts.js". You can make them match by changing either the file's name, or the "src" value in the HTML.

Also, the tag that loads the script is currently inside the "head" section of the HTML. But to be sure all the resources the script needs are available to it, it should be the very last thing in the "body" section.

Thank you so much all I had to do was change the position of the src. The snapshot I send you wasn't the original project. I was doing my project on brackets not on treehouse's workspace. I basically copied and pasted my project onto treehouse and forgot to change scripts name. like I said I just put the src at the end of the body like you told me. Thank you