/    Sign up×
Community /Pin to ProfileBookmark

need array script

Need to create a questionnaire that prompts visitors to my web page for personal information, 4 questions. I need to assign each piece of info to an array then use a for loop to print the info on the screen.

This is what I have so far and it isn’t working:

<HTML>

<HEAD>
<TITLE>Questionnaire</TITLE>

<SCRIPT LANGUAGE=”JavaScript”>
<!– HIDE FROM INCOMPATIBLE BROWSERS

////////NEED TO ARRAY QUESTIONS AND ASSIGN ANSWERS TO AN ARRAY

var msg = new Array(5), i = 0;
for(i=0; i<5; i++)
var info = prompt(msg[i]);
document.writeln(info[0]);
for(i=0; i<msg.length; i++) ;

msg[0] = “What’s your name?”;
msg[1] = “What’s your address?”;
msg[2] = “What kind of job do you have?”;
msg[3] = “What is your favorite movie?”;
msg[4] = “What is your favorite activity?”;

//STOP HIDING FROM INCOMPATIBLE BROWSERS –>
</SCRIPT>
</HEAD>
<BODY>
<H1>Questionnaire</H1>
</BODY>
</HTML>

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@fredmvDec 22.2003 — &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
var questions =
[
'What\'s your name?',
'What\'s your address?',
'What kind of job do you have?',
'What is your favorite movie?',
'What is your favorite activity?'
], answers = [];

<i> </i> for(i=0; i&lt;questions.length; i++) answers[i] = prompt(questions[i], '');

<i> </i> document.write('&lt;dl&gt;');

<i> </i> for(i=0; i&lt;answers.length; i++) document.writeln('&lt;dt style="font-weight: bold;"&gt;' + questions[i] + '&lt;/dt&gt;&lt;dd&gt;' + answers[i] + '&lt;/dd&gt;');

<i> </i> document.write('&lt;/dl&gt;');
<i> </i> //]]&gt;
<i> </i> &lt;/script&gt;
<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@1christineauthorDec 22.2003 — I added your code and I get a runtime error on line 10 character 11, expecting ].

I am writing this only in JavaScript (class I'm taking).

Can I add your code as is to my Javascript coding?
Copy linkTweet thisAlerts:
@fredmvDec 22.2003 — Just use my code "as is" and it will work fine.
Copy linkTweet thisAlerts:
@AdamBrillDec 22.2003 — [i]Originally posted by fredmv [/i]

[B]Just use my code "as is" and it will work fine. [/B][/QUOTE]
Wrong, but my guess is that it's not your fault, but rather the forum's fault. ? Change these lines:

'What's your name?',

'What's your address?',


To this:

'What's your name?',

'What's your address?',

My guess is you had those in there on your code, but the forums took it out. Either way, they need to be there...
Copy linkTweet thisAlerts:
@fredmvDec 22.2003 — Thanks for the correction Adam. Yes &#8212; they were of course there (I test all code before posting it). They really need to fix that. Next time I'll have to remind myself to escape my escapes. ?
Copy linkTweet thisAlerts:
@AdamBrillDec 22.2003 — [i]Originally posted by fredmv [/i]

[B]Next time I'll have to remind myself to escape my escapes.[/B][/QUOTE]
LOL, yeah, but if you ask me, it's pretty dumb that this forums does that. :rolleyes: I think it only does it if you put the code into a code block, though...
Copy linkTweet thisAlerts:
@1christineauthorDec 22.2003 — Hey, thanks.

The code does work.

The only thing is that this is for a class I'm taking and we haven't gone over some of this info.

How do I code it without this stuff and so that it's very basic:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title>untitled</title>

<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />

<script type="text/javascript">

//<![CDATA[


document.write('<dl>');

for(i=0; i<answers.length; i++) document.writeln('<dt style="font-weight: bold;">' + questions[i] + '</dt><dd>' + answers[i] + '</dd>');

document.write('</dl>');
//]]>
Copy linkTweet thisAlerts:
@AdamBrillDec 22.2003 — If you just put this into the body of any page, it should work fine:&lt;script type="text/javascript"&gt;
var questions =
[
'What\'s your name?',
'What\'s your address?',
'What kind of job do you have?',
'What is your favorite movie?',
'What is your favorite activity?'
], answers = [];
for(i=0; i&lt;questions.length; i++) answers[i] = prompt(questions[i], '');
document.write('&lt;dl&gt;');
for(i=0; i&lt;answers.length; i++) document.writeln('&lt;dt style="font-weight: bold;"&gt;' + questions[i] + '&lt;/dt&gt;&lt;dd&gt;' + answers[i] + '&lt;/dd&gt;');
document.write('&lt;/dl&gt;');
&lt;/script&gt;
fredmv, you confused them with your XHTML. ?
Copy linkTweet thisAlerts:
@1christineauthorDec 22.2003 — Why did you use:

document.write('<dl>');

('<dt style="font-weight: bold;">' + questions[i] + '</dt><dd>' + answers[i] + '</dd>');

document.write('</dl>');



What does '<dl>' mean?

and <dt>

and <dd>?



Thanks.
Copy linkTweet thisAlerts:
@fredmvDec 22.2003 — They're [url=http://www.w3.org/TR/REC-html40/struct/lists.html#h-10.3]definition lists[/url]. ?

It's the most semantically correct way to present this kind of information.
×

Success!

Help @1christine 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...