Reputation: 11
im trying to return the value of all the questions in my array and input them in my msg variable. i know im not writing the correct code in my template literal, could i get some help please? Thank you!
const questions = [
['How old are you?'],
['when were you born?'],
['Where are you right now?']
]
for ( var i = 0; i < questions.length; i++) {
var answers = prompt(questions[i]);
}
let msg = `You are ${answers[0]}, you used to live in ${answers[1]} and i can see you are in ${answers[2]}`;
document.querySelector('.display').innerHTML = msg;
Upvotes: 0
Views: 32
Reputation: 4306
const questions = [
['How old are you?'],
['when were you born?'],
['Where are you right now?']
]
let answers = [];
questions.forEach(question => answers.push(prompt(question[0])))
let msg = `You are ${answers[0]}, you used to live in ${answers[1]} and i can see you are in ${answers[2]}`;
document.querySelector('.display').innerHTML = msg;
I created an array that will be filled in the loop, eventually when the loop is done you'll be able to access it.
Upvotes: 1
Reputation: 1714
You are overwriting the answer on each loop. You need to store each answer in an array.
const questions = [
['How old are you?'],
['when were you born?'],
['Where are you right now?']
]
var answers = [];
for ( var i = 0; i < questions.length; i++) {
answers.push(prompt(questions[i]));
}
let msg = `You are ${answers[0]}, you used to live in ${answers[1]} and i can see you are in ${answers[2]}`;
document.querySelector('.display').innerHTML = msg;
Upvotes: 1