/    Sign up×
Community /Pin to ProfileBookmark

Concatenate the value of one variable to call another variable?

I am building a self scoring quiz that displays each question with the answer chosen by the user and a mark of right or wrong.

The values are declared in the form (quiz) that are passed to the score sheet eg:

<input onclick=”A0=’a’;A0a=’Aardvark'” type=”radio” name=”R1″ />
<input onclick=”A0=’b’;A0b=’Bat'” type=”radio” name=”R1″ />
<input onclick=”A0=’c’;A0c=’Cat'” type=”radio” name=”R1″ />
<input onclick=”A0=’d’;A0d=’Dog'” type=”radio” name=”R1″ />

Then when the user is done and clicks the check button the score sheet adds the correct answers by adding with an If statement that compares just the correct answer letter (a, b, c, or d) then adds just the correct ones using i=i+1.

Then use a document.write to write the question and the answer chosen to the score sheet to be viewed. Calling the answer letter with A0 is easy enough but I’m trying to call the values of A0a, A0b, A0c, A0d by concatenate the variable name for the answer text with this:

Answers.document.write(“+(“A0″+(A0.toLowerCase()))+”);

This outputs the right name of the variable name if the “a” radio button is clicked then the output shows “A0a” but I can’t get the value of A0a = “Aardvark” to be written to the score sheet.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangFeb 14.2009 — ? but this is what you require I think, gives 'Aardvark':window['AO'+AO]
document.write can not be used after the page loads, so is probably incorrect here.
Copy linkTweet thisAlerts:
@HegureRyuauthorFeb 15.2009 — Sorry, I was using document.write to write the information to a score sheet that opens in a popup separate from the quiz, but this looks like it is working. Thanks so much!
×

Success!

Help @HegureRyu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...