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

akash dikkaram
akash dikkaram
1,927 Points

Quiz Challenge, Part 2 - JavaScript Loops, Arrays and Objects I having It Wrong In the List!!

<html> <head></head> <body>

<h2>Javascript Quiz</h2>
<script>


var correct=[];
var wrong=[];
var html;


function list(array){
    var listhtml='<ol>';
    for(i=0;i<array.length;i++)
        {
            listhtml+='<li>'+array[i]+'</li>';
        }
    listhtml+='</ol>'
    return listhtml;
}





var qanda=[['States','29'],
         ['tamilnadu','chennai'],
         ['india','delhi']
        ];


for(i=0;i<qanda.length;i++)
{    
var response =prompt(qanda[i][0]);

if(response===qanda[i][1])
    {
       correct+=qanda[i][0];

    }
    else
        {

            wrong+=qanda[i][0];

        }

}
document.write('<h3>Thse are correctly answered</h3>')

document.write('<p>'+list(correct)+'</p>')
document.write('<h3>Thse are wrongly answered</h3>')
document.write('<p>'+list(wrong)+'</p>')   
</script>
</body>
</html>

1 Answer

To add an item to an array, pass it as an argument to the push method:

correct.push(qanda[i][0]);

This is because you can only add (+=) to variables that are of a certain type (numbers, strings, etc).