Learning Javascript. I'm stuck!

I’m trying to push an array into another. However it is not working.

var input = require('readline-sync');

class MCQ {
    constructor(question, choices, answer, category) {
        this.question = question;
        this.choices = choices;
        this.answer = parseInt(answer);
        this.category = parseInt(category);
    }
}//MCQ


class Quiz {
    constructor() {
        this.questionPool = []; //declare the question pool property of the quiz class
        this.mcqSelected = [];

        //populate 5mcqs and push them into array
        this.questionPool.push(new MCQ("A pull or a push applied or exerted on an object is", ["Energy", "Force", "Velocity", "Distance"], 1, 1));
        this.questionPool.push(new MCQ("What is the unit of force?", ["Degree", "Joule", "Newton", "Pascal"], 2, 1));
        this.questionPool.push(new MCQ("What is defined as mass times the distance.", ["Velocity", "Work", "Energy", "Temperature"], 1, 1));
        this.questionPool.push(new MCQ("When an object is in motion, what kind of energy does it possess?", ["Kinetic Energy", "Potential Energy", "Electrical Energy", "Chemical Energy"], 0, 1));
        this.questionPool.push(new MCQ("What is the force that holds back a motion", ["Gravity", "Friction", "Momentum", "Mass"], 1, 1));

        this.questionPool.push(new MCQ("A", ["Energy", "Force", "Velocity", "Distance"], 1, 2));
        this.questionPool.push(new MCQ("B?", ["Degree", "Joule", "Newton", "Pascal"], 2, 2));
        this.questionPool.push(new MCQ("C.", ["Velocity", "Work", "Energy", "Temperature"], 1, 2));
        this.questionPool.push(new MCQ("D?", ["Kinetic Energy", "Potential Energy", "Electrical Energy", "Chemical Energy"], 0, 2));
        this.questionPool.push(new MCQ("E", ["Gravity", "Friction", "Momentum", "Mass"], 1, 2));

        this.questionPool.push(new MCQ("1", ["Energy", "Force", "Velocity", "Distance"], 1, 3));
        this.questionPool.push(new MCQ("2?", ["Degree", "Joule", "Newton", "Pascal"], 2, 3));
        this.questionPool.push(new MCQ("3", ["Velocity", "Work", "Energy", "Temperature"], 1, 3));
        this.questionPool.push(new MCQ("4", ["Kinetic Energy", "Potential Energy", "Electrical Energy", "Chemical Energy"], 0, 3));
        this.questionPool.push(new MCQ("5", ["Gravity", "Friction", "Momentum", "Mass"], 1, 3));

        this.questionPool.push(new MCQ("6", ["Energy", "Force", "Velocity", "Distance"], 1, 4));
        this.questionPool.push(new MCQ("7", ["Degree", "Joule", "Newton", "Pascal"], 2, 4));
        this.questionPool.push(new MCQ("8", ["Velocity", "Work", "Energy", "Temperature"], 1, 4));
        this.questionPool.push(new MCQ("9", ["Kinetic Energy", "Potential Energy", "Electrical Energy", "Chemical Energy"], 0, 4));
        this.questionPool.push(new MCQ("10", ["Gravity", "Friction", "Momentum", "Mass"], 1, 4));


    }// constructor

    getNumberOfQuestion() {
        return this.questionPool.length;
    }

    // getQuestionAt(index) {
    //     return this.questionPool[index];
    // }


    pushSelectedQuestion() {
        for (var i = 0; i < this.getNumberOfQuestion; i++) {
            if (usercategory == this.questionPool[i].category) {
                this.mcqSelected.push(this.questionPool[i])
                return this.pushSelectedQuestion;
            }
        }

    }

}//Quiz

//------- main ----------------
var quiz = new Quiz();

function getName() {
    console.log("-= Welcome to Quiz Application =-");
    var name = input.question("please enter your name: ");
    var usercategory = parseInt(input.question("Hi " + name + ", please choose the quiz category you would like to attempt:" + "\n" + "\n" + "(1) Physic\n" + "(2) Chemistry\n" + "(3) History\n" + "(4) Geography\n" + ">> "));
    switch (usercategory) {
        case 1:
            console.log("The category you have selected is Physic")
            break;
        case 2:
            console.log("The category you have selected is Chemistry")
            break;
        case 3:
            console.log("The category you have selected is History")
            break;
        case 4:
            console.log("The category you have selected is Geography")
            break;

        default:
            break;
    }
}
function showquestion() {
    for (i = 0; i < 5; i++) {
        console.log(quiz.mcqSelected)
    }
}


getName();
quiz.pushSelectedQuestion();
// for (var i = 0; i < 5; i++) {
//     console.log(quiz.mcqSelected[i]);
// }
// console.log(quiz.mcqSelected);  // theres nothing inside the array

showquestion();
// console.log(questionSelected)
// showquestion();

I’m trying to get the selected category. Lets say physics, which is 1. I’m trying to push them from question.pool array to mcqselected array. However not sure where im wrong

getNumberOfQuestion is a method, not a property. You’ll need to call it instead of using it. i < this.getNumberOfQuestion()

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.