2

I am trying to figure out the best way to dynamically build variables. I have a database that looks like this:

One record:

ans1
ans2
ans3

res1
res2
res3

I have a function that checks the answer against the button the user pressed.

checkAnswer(q, a, $event) {
    if(a == q.correct){
        this.correctanswer = q.success
    }
}

The variable 'a' is either 1, 2 or 3

I would like the code to look something like this:

this.correctanswer = q.res+a

However, this is not working. How can I look up the appropriate response based on the answer the user chooses? So if the user presses answer 1 they see res1 and so on.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Jason
  • 1,091
  • 4
  • 19
  • 40

1 Answers1

3

Use [] (square bracket notation):

this.correctanswer = q["res" + a];
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79