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 Object-Oriented JavaScript (2015) Practice Project User Interface Code

alex gwartney
alex gwartney
8,849 Points

Quiz Project is not working correctly

So i managed to build the quiz application but what is happening is once i create more than two objects at a time the program completely crashes. Can some one take a look at it and see why it might be doing this?

//The arguments supply the followinng values.

//1:questions argument supplys a question that you want to ask your users
//2:awnser selection one argument supplys the user with the first posible awnser selction
//3:awnser selection two argument supplys the user with a secoend posible awnser selection
//4:correct awnser one and correct awnser two can be placed as correct or incorect to set the values //of the questions awnsers 
//to see weather or the users selection is correct.

function askquestion(questions,awnserselectionone,
                     awnserslectiontwo,correctawnserone,
                     correctawnsertwo){
    this.askquestions = questions;
    this.questionawnser= awnserselectionone;
    this.questionawnsertwo = awnserslectiontwo
    this.correctawnserone = correctawnserone;
    this.correctawnsertwo = correctawnsertwo;

}
askquestion.prototype.addquestion = function(){
    //stuff to set up tomarow take and incrment the array

    var point= 0;
    var wrong = 0;
    var quest = []

    var userchoicesone=[];
    var userchoicetwo = [];

    var userawnserscorrect = [];
    var userawnserscorrecttwo = [];

    quest.push(this.askquestions);

    userchoicesone.push(this.questionawnser);
    userchoicetwo.push(this.questionawnsertwo);

    userawnserscorrect.push(this.correctawnserone);
    userawnserscorrecttwo.push(this.correctawnsertwo);

    for(i=0; i<quest.length; i++){
        $("#question").html("your question is" + quest[i]);
    }

    for(i=0; i<userchoicesone.length; i++){
        $("#choice0").html("your question is" + userchoicesone[i]);
        var choice = userchoicesone[i];
    }

    for(i=0; i<userchoicetwo.length; i++){
        $("#choice1").html("your question is" + userchoicetwo[i]);
        //var choice = user[i];
    }

    $("#guess0").click(function(){
        for(i=0; i<userawnserscorrect.length; i++){
            if(userawnserscorrect[i]==="correct"){
                point+=1;
            }else{
                wrong+=1;
            }
        }

        $("#progress").html("Question" + "" + point + "of" + wrong )
    });



    $("#guess1").click(function(){
        for(i=0; i<userawnserscorrecttwo.length; i++){
            if(userawnserscorrecttwo[i]==="correct"){
                point+=1;
            }else{
                wrong+=1;
            }
        }
        $("#progress").html(" You got questions" + "" + point + "of" + wrong + "right")
    });



}
function createquiz(){
    var done = [
        new askquestion("question one","questiononeawnser","questionawnsertwo","correct","incorect"),
        new askquestion("question two","questiononeawnser three","questionawnser four","incorect","correct"),

    ]

    count =0;
    for(i=0; i<done.length; i++){
        done[count].addquestion()

        $("#guess0").click(function(){
            count+=1;
            done[count].addquestion()
        });
        $("#guess1").click(function(){
            count+=1;
            done[count].addquestion()
        });
    }
}

1 Answer

I'm sorry, but I don't really know where to start with your code... I think you would benefit a lot by going back through this course, follow along as close as possible in Workspaces with everything Andrew is doing, and have another shot at this project.

Or watch the solution video and implement something similar and try to figure out how each part works.

Also note that we weren't really meant to use jQuery in this project, but I guess that's up to you.

The one thing that does raise a flag in my mind from your code is the part at the end where you're adding click event handlers on each iteration of the loop... you will probably double up on them there, and perhaps having more questions is causing a large amount of duplicate event handlers to be added...