(function() {//give privacy

    
function Question(question, answers, correct){
    this.question = question;
    this.answers = answers;
    this.correct = correct;
}

Question.prototype.displayQ = function() {
    console.log(this.question);

    for(var i= 0; i<this.answers.length ; i++){
        console.log( i +":"+ this.answers[i]);
    }
}


Question.prototype.checkAnswer=
function(ans) {
    if(ans===this.correct){
        console.log('correct answer!');
    } else{
        console.log('you are wrong!!!');
    }
}

var q1 = new Question('Is JS coolest programing language in the world?', ['yes','no'], 0);

var q2 = new Question('What is the name of this course\'s teacher?', ['John','Micheal','Jonas'],2);

var q3 = new Question('What does best describr coding?', ['Boring', 'Hard', 'Fun','Tedious'],2);

var questions=[q1, q2, q3];

var n = Math.floor(Math.random()*questions.length);

questions[n].displayQ()

var answer = parseInt(prompt('Please select the correct answer.'));

questions[n].checkAnswer(answer);

})();

 

 

응용

(function() {//give privacy

    
    function Question(question, answers, correct){
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }
    
    Question.prototype.displayQ = function() {
        console.log(this.question);
    
        for(var i= 0; i<this.answers.length ; i++){
            console.log( i +":"+ this.answers[i]);
        }
    }
    

    Question.prototype.checkAnswer=
    function(ans, callback) {
        var sc;

        if(ans===this.correct){
            console.log('correct answer!');
            sc=callback(true);
        } else{
            console.log('you are wrong!!!');
            sc=callback(false);
        }
        this.displaySc(sc);

    }
    
    Question.prototype.displaySc = 
    function(score){
        console.log('your score is '+score);
        console.log('----------------------');
           
    }

    var q1 = new Question('Is JS coolest programing language in the world?', ['yes','no'], 0);
    
    var q2 = new Question('What is the name of this course\'s teacher?', ['John','Micheal','Jonas'],2);
    
    var q3 = new Question('What does best describr coding?', ['Boring', 'Hard', 'Fun','Tedious'],2);
    
    var questions=[q1, q2, q3];
   
    function score() {
        var sc = 0;
        return function(correct){
            if(correct){
                sc++;
            }
            return sc;
        }
    }

    var keepSc = score();

    function nextQ() {

        var n = Math.floor(Math.random()*questions.length);
    
        questions[n].displayQ()
    
        var answer = prompt('Please select the correct answer.');

        if(answer !== 'exit'){
            questions[n].checkAnswer(parseInt(answer), keepSc);
            nextQ();
        }        
    }
    nextQ();
})();
    
    
    

'javascript' 카테고리의 다른 글

JavaScript/ ES5 vs ES6  (0) 2020.03.09
JavaScript/ let , const & var  (0) 2020.03.09
JavaScript/ Bind, Call and Apply  (0) 2020.02.24
JavaScript/ Closure  (0) 2020.02.24
JavaScript/ Immediately Invoked Function Expressions [IIFE]  (0) 2020.02.24