Difference between revisions of "Coding Challenge1"

From rbachwiki
Jump to navigation Jump to search
 
Line 15: Line 15:


7. Suppose this code would be a plugin for another programmer to use in their code. so make sure that all your code is private and doesn't interfere with the other programmers code. <br>
7. Suppose this code would be a plugin for another programmer to use in their code. so make sure that all your code is private and doesn't interfere with the other programmers code. <br>
<pre>
// function constructor
(function() {
    function Question(question, answer, correct) {
        this.question = question;
        this.answer = answer;
        this.correct = correct;
    }
    Question.prototype.displayQuestion = function() {
            // shows the question
            console.log(this.question);
            // loops through the answers and display them
            for (var i = 0; i < this.answer.length; i++) {
                console.log(i + ' : ' + this.answer[i]);
            }
        }
        // checks if the correct answer was passed
    Question.prototype.checkAnswer = function(ans, callBack) {
        var sc;
        if (ans === this.correct) {
            console.log('Correct Answer');
            sc = callBack(true);
        } else {
            console.log('incorrect, try again');
            sc = callBack(false);
        }
        this.displayScore(sc);
    }
    // display score
    Question.prototype.displayScore = function(score) {
            console.log('Your current score is: ' + score);
            console.log('--------------');
        }
        // string array int
    var q1 = new Question('Is Java script the coolest', ['yes', 'No'], 0);
    var q2 = new Question('What is my name', ['john', 'robert', 'harry'], 1);
    var q3 = new Question('what day is today', ['mon', 'tues', 'wed'], 2);
    var questions = [q1, q2, q3];
    function score() {
        var sc = 0;
        return function(correct) {
            if (correct) {
                sc++;
            }
            return sc;
        }
    }
    var keepScore = score();
    function nextQuestion() {
        // get random number for quesions
        var n = Math.floor(Math.random() * questions.length);
        questions[n].displayQuestion();
        // get the results of the prompt
        var answer = prompt("Please select the correct answer");
        // of not exit continue
        if (answer !== 'exit') {
            questions[n].checkAnswer(parseInt(answer), keepScore);
            nextQuestion();
        }
    }
    nextQuestion();
})();
</pre>


==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Latest revision as of 14:32, 9 November 2016

1. Build a function constructor called question to describe a question. A question should include
a. Question itself
b. the answer from which the player can choose the correct one ( choose and adequate data structure here, array object etc.)
c. correct answer

2. Create a couple of questions using the constructor

3. Store them all inside the array

4. Select one random question and log it to the console. together with the possible answers (each question should have a number

5. Use the 'prompt' function to ask the user for the correct answer. the user should input the number of the correct answer such as you displayed on task 4.

6. Check if the answer is correct and print to the console whether the answer is correct or not

7. Suppose this code would be a plugin for another programmer to use in their code. so make sure that all your code is private and doesn't interfere with the other programmers code.

// function constructor

(function() {
    function Question(question, answer, correct) {
        this.question = question;
        this.answer = answer;
        this.correct = correct;
    }
    Question.prototype.displayQuestion = function() {
            // shows the question
            console.log(this.question);
            // loops through the answers and display them
            for (var i = 0; i < this.answer.length; i++) {
                console.log(i + ' : ' + this.answer[i]);
            }
        }
        // checks if the correct answer was passed
    Question.prototype.checkAnswer = function(ans, callBack) {
        var sc;
        if (ans === this.correct) {
            console.log('Correct Answer');
            sc = callBack(true);
        } else {
            console.log('incorrect, try again');
            sc = callBack(false);
        }
        this.displayScore(sc);
    }


    // display score
    Question.prototype.displayScore = function(score) {
            console.log('Your current score is: ' + score);
            console.log('--------------');
        }
        // string array int
    var q1 = new Question('Is Java script the coolest', ['yes', 'No'], 0);
    var q2 = new Question('What is my name', ['john', 'robert', 'harry'], 1);
    var q3 = new Question('what day is today', ['mon', 'tues', 'wed'], 2);
    var questions = [q1, q2, q3];

    function score() {
        var sc = 0;
        return function(correct) {
            if (correct) {
                sc++;
            }
            return sc;
        }
    }

    var keepScore = score();

    function nextQuestion() {
        // get random number for quesions
        var n = Math.floor(Math.random() * questions.length);

        questions[n].displayQuestion();
        // get the results of the prompt
        var answer = prompt("Please select the correct answer");
        // of not exit continue
        if (answer !== 'exit') {

            questions[n].checkAnswer(parseInt(answer), keepScore);
            nextQuestion();
        }


    }
    nextQuestion();
})();

Back To Top- Home - Category