# Thread: JavaScript for Survey Results

1. Registered User
Join Date
Jul 2011
Posts
9

## 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.

2. Registered User
Join Date
Jul 2011
Posts
6
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.

3. Originally Posted by Hullabaloo
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.

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?

4. Registered User
Join Date
Jul 2011
Posts
9
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.

5. Registered User
Join Date
Jul 2011
Posts
9
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">
<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>
<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>
</p>
<p><label>Question 2</label>
<br>
</p>
<p><label>Question 3</label>
<br>
</p>
<p><label>Question 4</label>
<br>
</p>
<p><input type="button" value="Go" onclick="validateform()"></p>
</form>
</body>
</html>

6. 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.

Code:
```<!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">
<title> q1 Site q2 </title>
<script type="text/javascript">
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<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
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 < 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
*/
</script>
<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>
</p>
<p><label>Question 2</label>
<br>
</p>
<p><label>Question 3</label>
<br>
</p>
<p><label>Question 4</label>
<br>
</p>
<p><input type="button" value="Go" onclick="validateform()"></p>
</form>
</body>
</html>```
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.

7. Registered User
Join Date
Jul 2011
Posts
9
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.

8. Registered User
Join Date
Jul 2011
Posts
9

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!
Last edited by Hullabaloo; 08-16-2011 at 05:13 PM.