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 Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Himanshu Chopra
Himanshu Chopra
3,055 Points

Can these 5 question appear randomly each time when we load the page?

Well actually I want each time when user refreshes the page the questions order should change every time. so can these question appear randomly each time when we load the page? can it be done by Math.random or something. I have no clue how to do this any help is highly appreciated. here is my workspace

https://teamtreehouse.com/workspaces/11348572

Hi Himanshu,

The Workspace seems to be down. Can you please fix the link?

Best Regards,

Philip

3 Answers

Bryant Feld
seal-mask
.a{fill-rule:evenodd;}techdegree
Bryant Feld
Full Stack JavaScript Techdegree Student 26,144 Points

create an array of questions and answer pairs, the use random to select a question answer pair from the array, do you need an actual code example?

Himanshu Chopra
Himanshu Chopra
3,055 Points

Thanx Bryant! It would be great If you can explain it with code. can you view https://teamtreehouse.com/workspaces/11348572 this workspace?

Nestor Segura
Nestor Segura
13,025 Points

Hi,

I highly recomend you this course https://teamtreehouse.com/library/objectoriented-javascript.

Using the last practice exercise I made this modifications to that code

create this.questionsLeft = 5;

changed this.currentQuestionIndex = parseInt(Math.random()*4);

and implement those 2 on the rest of the functions.

function Quiz(questions) {
    this.score = 0;
    this.questions = questions;
    this.currentQuestionIndex = parseInt(Math.random()*4);
    this.questionsLeft = 5;
}

Quiz.prototype.guess = function(answer) {
    if(this.getCurrentQuestion().isCorrectAnswer(answer)) {
        this.score++;
    }
    this.currentQuestionIndex++;
    this.questionsLeft--;
};

Quiz.prototype.getCurrentQuestion = function() {    
    if (this.currentQuestionIndex == questions.length){
        if(this.questionsLeft >= 1){
            this.currentQuestionIndex = 0;
        }
    }
    return this.questions[this.currentQuestionIndex];
};

Quiz.prototype.hasEnded = function() {
    return this.questionsLeft == 0;
};

I hope it helps

Himanshu Chopra
Himanshu Chopra
3,055 Points

Thank You Nestor! Actually I am learning this basic Javascript course for now do you think It would be okay for me to directly jumping into the objectoriented javascript?

Thanx for the help!

Nestor Segura
Nestor Segura
13,025 Points

It is better that you do all thrue the courses. There is a Javascript track. Because object ist intermediate level already.

Good look ano have fun learning!

Himanshu Chopra
Himanshu Chopra
3,055 Points

Thanx Nestor! Yes I am doing this track https://teamtreehouse.com/tracks/fullstack-javascript. currently I am doing functions.

Thank you:)