Does JavaScript keep a log of a single variables different states? -


i trying build quiz and, on completion, display of user's answers without creating different variables store each one.

e.g.,

var state1 = {     name: 'delaware',     capital: 'dover',     orderofentry: 1, };  var useranswer.quiz = prompt('what state capital of ' + state1.name +'?');  var state2 = {     name: 'pennsylvania',     capital: 'harrisburg',     orderofentry: 2, };  var useranswer.quiz = prompt('what state capital of ' + state2.name +'?'); 

instead of creating new variable each answer, access answers in order given, array index.

you can store in array:

var answers = [{     name: 'delaware',     capital: 'dover' }, {     name: 'pennsylvania',     capital: 'harrisburg' }]; var i;  (i = 0; < answers.length; i++) {     answers[i].answer = prompt('what state capital of ' + answers[i].name + '?'); } 

now each object in answers have answer property contains user-supplied value each prompt.


Comments