Si selecciono "q1" de las preguntas, ¿cómo selecciono también la primera matriz de las respuestas?
Este es mi código ahora mismo:
questions = ["q1", "q2", "q3"] answers = [ ["r", "f", "f", "f"], ["r2", "f2", "f2", "f2"], ["r3", "f3", "f3", "f3"] ]; function pickQuestion() { if (questions.length > 0) { question = questions[Math.floor(Math.random() * questions.length)]; questions.splice(questions.indexOf(question), 1); // find array in answers that matches question and remove it answers.splice(answers.indexOf(question), 1); return ([question, answerArray]); } else { return ("End of questions"); }}Aquí tu código en funcionamiento ;)
questions = ["q1", "q2", "q3"] answers = [ ["r", "f", "f", "f"], ["r2", "f2", "f2", "f2"], ["r3", "f3", "f3", "f3"] ]; function pickQuestion() { if (questions.length > 0) { randomIndex = Math.floor(Math.random() * questions.length) question = questions[randomIndex]; let answer = answers[randomIndex] // Remove played question/answer questions.splice(randomIndex, 1); answers.splice(randomIndex, 1); return ([question, answer]); } else { return ("End of questions"); } } let a = pickQuestion() console.log(a)function pickQuestion() { if (questions.length) { // set the index to a random number let index = Math.floor(Math.random() * questions.length); const question = questions[index]; const answerArray = answers[index]; questions.splice(index, 1); answers.splice(index, 1); return ([question, answerArray]); } else { return ("End of questions"); } }