vendredi 31 mars 2023

how do I get to a random index in a object that holds a array of objects and get a specific property and append that property content?

I'm trying to make a trivia game as a project at my bootcamp I'm still new to coding but I get some basic stuff and I want to use Dom manipulation to grab a object from state that holds a questions array of objects and I'm using the form element in html to make the textContent of the label element in the form to be the question property which is a string and evaluate the input to the answer that I put in my trivia question constructor. I have tried everything i learned so far but I'm getting no where.

class Question {
  constructor(question, answer, hint) {
    this.question = question;
    this.answer = answer;
    this.hint = hint;
    this.attempts = 3;
    state.questions.push(this);
  }
}
// helper function for generating a random question from the state index.
function getRandomQuestion() {
  return Math.floor(Math.random() * state.questions.length);
}
function renderQuestion() {

  let currentQuestion = getRandomQuestion();
  for (let i = state.questions.length; i === 0; pocketArray.push(state.questions[currentQuestion])) {
    while (state.questions[currentQuestion].attempts > 0) {
      question.innerText = state.questions[currentQuestion].question;
    }
  }
}
     <form class="current-question">
                <label for="userInput" id="question"></label>
                <input type="text" name="userInput" id="user">
                <button type="button" for="userInput" id="submit">Submit</button>
            </form>



Aucun commentaire:

Enregistrer un commentaire