/    Sign up×
Community /Pin to ProfileBookmark

JavaScript for Survey Results

Hi all!

I’m a student, and I’m BRAND NEW to JavaScript, so it’s safe to assume I know only slightly more about this stuff than your grandma ?

For a class final, I’m trying to develop a survey that returns results based on user answers. The survey is four yes/no questions, and based on how users answer those questions, they receive a result I wrote specifically for that yes/no combination.

Example:
Yes, Yes, Yes, Yes – returns result A
Yes, Yes, Yes, No – returns result B
et cetera for every possible combination 1-16

How would I go about building something like this? It would be great if the script also displayed the survey results on the result page, but I could use HTML to do that.

THANK YOU IN ADVANCE FOR YOUR HELP!!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@igordoninJul 27.2011 — I can't try to write that code for you right now 'cause I'm so tired, but I do have an idea of how I'd do it... I remember my exams in schools were multiple answers, something like this:

(Q) What are the properties of water?

1. it's tasteless

2. it smells funny

4. it's odorless

8. it's kinda green

16. it's colorless

32. it may only be found in liquid state

64. it's considered the universal solvent

128. etc

256. etc

so, the right answer would be options 1, 4, 16, and 64. sum that up: 85.

that way, you can always know what the person has chosen, by subtracting whatever is the highest possible alternative without making it less than 1.

example: john only checked 1,4, and 64, and thus added to 69. You would never think he had checked 128. Not even if he had chosen all the options before, which would add to 127. Got it?

anyways, that's just a way to treat the logics...

sorry for my bad English, hope I could help.
Copy linkTweet thisAlerts:
@JMRKERJul 27.2011 — Hi all!

I'm a student, and I'm BRAND NEW to JavaScript, so it's safe to assume I know only slightly more about this stuff than your grandma ?

For a class final, I'm trying to develop a survey that returns results based on user answers. ...

How would I go about building something like this? It would be great if the script also displayed the survey results on the result page, but I could use HTML to do that.

THANK YOU IN ADVANCE FOR YOUR HELP!![/QUOTE]

?

Before giving any answers, how about you give us a first attempt at solving the problem?

If it is a class final, is it some sort of take-home exam?

If you have already submitted it, what did your effort look like?

:eek:
Copy linkTweet thisAlerts:
@HullabalooauthorJul 27.2011 — Thanks so much for the reply, igordonin!

The quiz I'm trying to make doesn't have any right or wrong answers. The four yes/no questions are situational questions, and I wrote situational advice for each of the 16 possible (2x2x2x2) yes/no combinations.

I'm thinking the script could be a if...if else...if else...else script that links to the HTML page I wrote based on the specific yes/no combination the user selected.

Or maybe the link to the HTML page could appear when the user completes the quiz. Maybe JS could control the type of link that appears based on the user's yes/no combo?

Ugh, I'm in over my head ? It's due TOMORROW (yeah I'm a procrastinator), so I'll let you know what kind of rickety solution I come up with.
Copy linkTweet thisAlerts:
@HullabalooauthorJul 27.2011 — Here's the code I'm working with so far.

The logic is - hey, take these values from the HTML radio button form, and if these certain values are checked, open this certain page in a new window.

Two problems

1. This script doesn't actually do anything and I don't know why ?

2. I don't know how to refer to radio buttons with JavaScript.

Enough yapping, this is my best shot so far.

<!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" lang="en" xml:lang="en">

<head>

<title>

q1 Site q2

</title>

<script type="text/javascript">

function validateform() {

if (document.form.q1.Y1.checked && document.form.q2.Y2.checked && document.form.q3.Y3.checked && document.form.q4.Y4.checked)

{

window.open ("yes.html");

}

else if (document.form.q1.N1.checked && document.form.q2.Y2.checked && document.form.q3.Y3.checked && document.form.q4.Y4.checked)

{

window.open("noyesyesyes.html");

}

else

{

window.open("nonoyesyes.html");

}

}

/*code will be much longer when fully written for every possibility, but I'm trying to make it work on a small scale first*/

</script>

</head>

<body>

<h1>

q1 Site q2

</h1>

<h2>

Title

</h2>

<p>

Each yes/no answer combination produces a different result.

<p>

<form>

<p><label>Question 1</label>

<br>

<input type="radio" name="q1" value="Y1" />Yes

<input type="radio" name="q1" value="N1" />No

</p>

<p><label>Question 2</label>

<br>

<input type="radio" name="q2" value="Y2" />Yes

<input type="radio" name="q2" value="N2" />No

</p>

<p><label>Question 3</label>

<br>

<input type="radio" name="q3" value="Y3" />Yes

<input type="radio" name="q3" value="N3" />No

</p>

<p><label>Question 4</label>

<br>

<input type="radio" name="q4" value="Y4" />Yes

<input type="radio" name="q4" value="N4" />No

</p>

<p><input type="button" value="Go" onclick="validateform()"></p>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@JMRKERJul 27.2011 — I have a feeling that the instructor will not accept your submission if you use this because it is very simple,

but complex enough that you might not be able to explain how it works to him,

and I hope you do not plan on making computer science a major for your education.

<i>
</i>&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" lang="en" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt; q1 Site q2 &lt;/title&gt;
&lt;script type="text/javascript"&gt;
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i&lt;sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above <br/>
return str;
}

function validateform() {
var q1 = getRBtnName('q1');
var q2 = getRBtnName('q2');
var q3 = getRBtnName('q3');
var q4 = getRBtnName('q4');
var qans = q1+q2+q3+q4;
if (qans.length &lt; 4) { alert('Missing selection'); return; }

alert(qans+'.html'); // for testing purposes only
// document.location.href = qans+'.html';
// window.open(qans+'.html");
}

/*code will be much longer when fully written for every possibility,
but I'm trying to make it work on a small scale first
*/
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;
q1 Site q2
&lt;/h1&gt;
&lt;h2&gt;
Title
&lt;/h2&gt;
&lt;p&gt;
Each yes/no answer combination produces a different result.
&lt;p&gt;
&lt;form&gt;
&lt;p&gt;&lt;label&gt;Question 1&lt;/label&gt;
&lt;br&gt;
&lt;input type="radio" name="q1" value="Y" /&gt;Yes
&lt;input type="radio" name="q1" value="N" /&gt;No
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Question 2&lt;/label&gt;
&lt;br&gt;
&lt;input type="radio" name="q2" value="Y" /&gt;Yes
&lt;input type="radio" name="q2" value="N" /&gt;No
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Question 3&lt;/label&gt;
&lt;br&gt;
&lt;input type="radio" name="q3" value="Y" /&gt;Yes
&lt;input type="radio" name="q3" value="N" /&gt;No
&lt;/p&gt;
&lt;p&gt;&lt;label&gt;Question 4&lt;/label&gt;
&lt;br&gt;
&lt;input type="radio" name="q4" value="Y" /&gt;Yes
&lt;input type="radio" name="q4" value="N" /&gt;No
&lt;/p&gt;
&lt;p&gt;&lt;input type="button" value="Go" onclick="validateform()"&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;



Note also that there are no need for nested "if...then...else" statements.

If that was part of the assignment, then you are just out of luck.
Copy linkTweet thisAlerts:
@HullabalooauthorJul 27.2011 — JMRKER,

Thank you!

Your code works, even though I don't know why! And you definitely called it, I am not a computer science major. I'm an English major and this is way out of my league.

And don't worry...writing JS was not really the focus of this class.

Would you like to be cited for this code?

I can't thank you enough. I was up a nasty creek without a paddle.
Copy linkTweet thisAlerts:
@HullabalooauthorAug 16.2011 — Thanks again for all your help with me on this forum. I'm still new at this stuff and it's taking me some time to get used to it ?

How would I modify this code so that the quiz returns results in the same window instead of opening a new window every time?

WAIT A SECOND. I am kind of silly. The answer is

document.location.href = qans+'.html'

such a noob. Okay, nevermind. Thanks for this awesome forum!
×

Success!

Help @Hullabaloo 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.19,
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,
)...