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

Completed Quiz challenge in the Arrays section. Didn't use last function, called array w/join & tags passed to prnt().

What is the standard when it comes to passing array elements to a web page. Since I did'nt use listHTML variable that was used to build the html list. I was curious if what I did would be a problem in the real world of programming?

Here is my code just to clarify what I did. It really is similar except for the printing of results to webpage.

var questions = [
    ['What is the Texas capital?', 'Austin'],
    ['What is the largest Texas city?', 'Houston'],
    ['What is the western most Texas city?', 'El Paso'],
];

var correct = [];
var wrong = []; 
var correctAnswers = 0; 
var question;
var answer;
var response;
var listHTML  //I didn't use this variable so it would be removed in real world.  Just wanted to note this.

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

for(var i = 0; i < questions.length; i += 1) {
    question = questions[i][0];
    answer = questions[i][1];
    response = prompt(question);  //If answers are numbers the parseInt would be used.
    if (response === answer) {
        correctAnswers += 1;correct.push(question); 
        //console.log(correctAnswers + " " + response + " " + correct[i]);
            } 
        else {
                wrong.push(question); 
                //console.log(correctAnswers + " " + response + " " + wrong[i]);

            }
        }

html = 'You got ' + correctAnswers + ' question(s) right.' + 
        '<h2> </br> You got the following questions correct. </br> </h2>' + 
        correct.join(' </br> ') + 
        '<h2> </br> You got the following questions wrong. </br> </h2>' + 
        wrong.join(' </br> ');

print(html);

1 Answer

I'm not sure if you're looking to only use JavaScript, but most people use JQuery to update html. Of course you definitely can update web pages using only straight JavaScript.

In JQuery you'd identify the HTML element you want to update and then use the html (or append, prepend, etc. depending on what you want to do) method to update it.

$("#my-div").html(html)

Thanks Jason, I am just learning so am looking for feedback that will further me along with using JS and build on that with other tools such as JQuery. Thanks again.