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

jose macedo
jose macedo
6,041 Points

help with javascript.

should my javascript already work? and whats wrong with it

var pos = 0, test, test_staus, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
    ["what is 2 + 2", "4", "2", "22", "A"],
    ["what is 4 x 4", "14", "44", "16", "C"],
    ["what is -2087 + 20", "2050", "2067", "-2067", "C"],
    ["what computer language lets you style a web page", "C#", "CSS", "Python", "B"],
    ["which is the biggest planet inour solar system?", "Mercury", "Mars", "Pluto", "A"]
];
var message = document.getElementById("test");
function renderQuestion() {

    for (var i = 0; i > questions.legnth; i + 1) {
            question = questions[0][i];
            message.innerHTML = question;
    }
}

1 Answer

There are a few errors here and there that prevent this from running.

I noticed that in the for loop, you are trying to loop while i is greater than (>) questions.length, however, i starts at 0 meaning that the loop would immediately not run.

Additionally, there is a small typo for the length of the questions array (.legnth instead of .length).

Last but not least, as you loop through your array, you are keeping the first item (at index 0) throughout the loop, meaning that you would never leave the first 'question' array item:

 var someArray = [
    ["what is 2 + 2", "4", "2", "22", "A"],
    ["another question", "several", "fake", "answers"]
]

/// someArray[0][0] === "what is 2 + 2"
/// someArray[0][1] === "4"
/// someArray[0][2] === "2"
/// etc. etc.

but would iterate through its contents instead.

I have corrected those mistakes in the code below. Does this help you accomplish what you were going for?

 for (var i = 0; i < questions.length; i++) {
     question = questions[i][0];
     message.innerHTML = question;
 }