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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

don't see why this function is returning "undefined"

I have passed it an array, called it, logged it...the only error I get is undefined. The function celestialList() is the one giving me problems. If I comment out the call to that function, the rest of the code runs fine. Insights welcome :D

function print(output){
    document.getElementById("display").innerHTML = output;
}

//1 build an array of questions and answers 
//2 loop through Array
//3 prompt questions 
//4 compare to answers 
//5 track and store answers 
//6 print results

var celestialBodies = [
    ["What planet do we live on?", "earth"],
    ["What satelite circles the Earth?", "moon"],
    ["What warms the earth?", "sun"]
]

function celestialList(list){
    var html = "<ol>"
    var i;
    for(i = 0; i < list; i += 1){
        html += "<li>" + list[i][0] + list[i][1] + "</li>";
    }
    html += "</ol>"
    print(html)
}
var correctAnswers = [];
var incorrectAnswers = [];
var correctCount = 0;
var incorrectCount = 0;
var html = "";

    for(var i = 0; i < celestialBodies.length; i += 1){
        var answer = prompt(celestialBodies[i][0])
        answer = answer.toLowerCase();
        if(answer === celestialBodies[i][1]){
            alert("That's right");
            correctCount += 1
            correctAnswers.push(celestialBodies[i][0])
        }else {
            alert("Sorry")
            incorrectCount += 1;
            incorrectAnswers.push(celestialBodies[i][0])
        } /* if else */
    } /* for */
    html += "<h3>You got these answers right: </h3>"  
  + correctAnswers.join("<br>")
    + "<h3> You got these answers wrong; </h3>"
    + incorrectAnswers.join("<br>")
    + "<h3> In Summary:</h3>"
    + correctCount + " correct answers and <br>"
    + incorrectCount + " wrong answers.";
    print(html)
    console.log(celestialList(correctAnswers))
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! Sorry. john larson You're absolutely correct! Woops I missed the print function. And the reason I missed it is because I was running it in a test environment where I didn't have an ID called "display". So I retract that statement.

I'm rerunning your code and looking at why it's returning undefined!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Well I found one problem in your for loop you say to run while i is less than list. This should be while i is less than list.length.

function celestialList(list){
    var html = "<ol>"
    var i;
    for(i = 0; i < list.length; i += 1){  //This line! :)
        html += "<li>" + list[i][0] + list[i][1] + "</li>";
    }
    html += "</ol>"
    print(html)
}

However, I'm still getting a log out to the console I wasn't expecting. Still working on it. By the way, sorry. I forgot that removing my answer would remove your comments. I sincerely apologize :(

how could I miss .length I'll fix that and look at it some more. Thanks again for your help. Sometimes when things don't work like I expect I just start all over. This time I really wanted to see the kinds of things I'm missing.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again! Remember also, that this code isn't ever going to return earth, moon, and sun. Because it's the questions being pushed into correctAnswers. But if you change your code to this:

function celestialList(list){
    var html = "<ol>";
    var i;
    for(i = 0; i < list.length; i += 1){
        html += "<li>" + list[i] + "</li>";
    }
    html += "</ol>";
    return html;
}

It will at least log out the questions you got correct :thumbsup:

Jennifer, thanks for all your help. The best answer button is gone? I wanna click it for all the time you spent but...I don't know where it is! If you know how to get it back, I'll click it :D (I don't know what it does for you guys but it at least seems moderately important to some of you)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

john larson a best answer can only be selected for an answer. You and I have been communicating in comments because I removed my original answer (due to the whole not seeing the print function). That being said, best answers cannot be given in the general discussion category. I'm just happy to help where/if I can :sparkles:

Also, being able to answer someone else's questions truly reinforces your learning. It's one thing to produce it, and another thing to explain why something is the way it is. I've found that I've learned tons just by answering other people's questions and seeing if I can give decent examples, or walk through code line by line.