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.
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?
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!!
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?
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.
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>
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">
<head>
<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>
</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="Y" />Yes
<input type="radio" name="q1" value="N" />No
</p>
<p><label>Question 2</label>
<br>
<input type="radio" name="q2" value="Y" />Yes
<input type="radio" name="q2" value="N" />No
</p>
<p><label>Question 3</label>
<br>
<input type="radio" name="q3" value="Y" />Yes
<input type="radio" name="q3" value="N" />No
</p>
<p><label>Question 4</label>
<br>
<input type="radio" name="q4" value="Y" />Yes
<input type="radio" name="q4" value="N" />No
</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.
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.
Bookmarks