0

Am I able to switch this .forEach to a for loop? Curious if I could do the same thing with a for loop instead

 choices.forEach((choice) => {
        const number = choice.dataset['number'];
        // This will populate the choices into the "choice-option" in the html
        choice.innerText = currentQuestion['choice' + number];
    });
    // This removes the old question and adds a new one//
    // .splice method on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice//
    unusedQuestions.splice(questionIndex, 1);
    acceptAnswers = true;
};

thanks!

hannahatl
  • 91
  • 1
  • 8
  • 2
    you absolutely can - and probably should! have you tried to convert it? – Daniel A. White Jul 18 '21 at 13:31
  • 2
    Yes, you can but why switch to `for..loop` when you have declarative syntax with `forEach`. – DecPK Jul 18 '21 at 13:33
  • 1
    `for {let choice of choices){ // your existing code }` see: [For-each over an array in JavaScript](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) for detailed discussion. – pilchard Jul 18 '21 at 13:36

0 Answers0