简体   繁体   中英

How to loop through instances of an Object that are pushed into an array?

My goal is to make a game which asks the user 3 questions through random generation. I approached this by making a class and making new instances of questions and pushing them into an array. After that i made a function that would 'prompt' each question through the array and ask the user to enter the correct answer. If the user does not enter the correct answer, it gives the user 1 more opportunity to guess the answer correctly, else the user looses.

In this code i am pushing 3 questions into the questions array and i made a line of code "const randomVal = Math.floor(Math.random() * questions.length);" to generate random elements of the array.

My goal is to print all 3 questions that are inserted into the array RANDOMLY but my code only prints 1 random question, and then breaks.

I have tried using a for loop in the Questionz function but instead of printing 3 different questions, my loop prompts the same question 3 times.

// Creating a class

class Quiz
{
  constructor(ti,opA,opB,opC,ans) // Easy
  {
    this.title = ti;
    this.optionA = opA;
    this.optionB = opB;
    this.optionC = opC;
    this.answer = ans;
  }

}


// Making an array that will hold all the questions and options
const questions = [];
questions.push(new Quiz("Who is the greatest laker of all time?","Kobe", 
"Shaq", "Magic", "Kobe"));

questions.push(new Quiz("Who is the greatest hockey player of all 
time?","Crosby", "Ovechkin", "Kessel", "Crosby"));

questions.push(new Quiz("What is Torontos Baseball team called?","Blue 
Jays", "Rex Sox", "Yankees", "Blue Jays"));

const randomVal = Math.floor(Math.random() * questions.length);
let que1; // This is global
let i=0;

function Questionz() // Easy Questions (lvl 1)
{
que1 = prompt(`Q. ${questions[randomVal].title} 
\n\n1.${questions[randomVal].optionA} 
\n2.${questions[randomVal].optionB}\n3.${questions[randomVal].optionC}`);

Validation(randomVal);
}

// BOTTOM FUNCTION GIVES PROMPTED VALUE VALIDATION

function Validation(randomVal)
{
while(que1 !== questions[randomVal].answer)
{
  que1 = prompt(`${que1} is Incorrect!\n\nPlease try again!`);
  i++;

  if(que1 === questions[randomVal].answer)
  {
    alert("Correct!\n\nPress OK to move onto the next question!");
  }
  else if(i===1)
  {
    alert(`${que1} is incorrect.\n\nYou have lost.`);
    break;
  }
}
}

Questionz();

You are going to want to generate the random number EVERY TIME in the loop not once on page load. This function loops 3 times and creates 3 random numbers.

function Questionz() // Easy Questions (lvl 1)
{

    for(let z = 0; z < questions.length; z++) {
        let randomVal = Math.floor(Math.random() * questions.length);
        que1 = prompt(`Q. ${questions[randomVal].title} 
        \n\n1.${questions[randomVal].optionA} 
        \n2.${questions[randomVal].optionB}\n3.${questions[randomVal].optionC}`);

        Validation(randomVal);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM