mattasouths
Programmer
hi, i am writing a program which gives a list of questions and a boolean is recorded wen each question is answered this is the code for the program:
<script>
var z;
var active = 1;
var x = 0;
var y = 2;
var number
var q = new Array();
q[0]
q[1]
q[2]
function start_game() {
alert ("think of a number between 0 and 9 and answer the following questions by clicking on the yes or no buttons");
questions();
}
function questions() {
var question_yes = new Array(); // yes questions
question_yes[0] = "Is the number less than 5?";
question_yes[1] = "Is the numbers square less than 5?";
question_yes[2] = "Is half the number NOT a whole number?";
question_yes[3] = "Is it even?";
var question_no = new Array(); // no questions
question_no[0] = "Is the number less than 5?";
question_no[1] = "Is half the number less than 3?";
question_no[2] = "Is it odd?";
question_no[3] = "Is it's square root a whole number?";
if (z) { // check if previous question was true
alert(question_yes[x])
}
else {
alert(question_no[x])
}
}
function answer(answer) {
if (active) {
z = answer;
q[x] = answer;
if (x == y) {
check();
}
else {
x++
questions();
}
}
else {
alert("to play click the 'start game' button");
}
}
function check() {
if (q[0] && q[1] && q[y]) {
number = 1;
}
else if (q[0] && q[1] && q[y] == 0) {
number = 2;
}
else if (q[0] && q[1] == 0 && q[y]) {
number = 3;
}
else if (q[0] && q[1] == 0 && q[y] == 0) {
number = 4;
}
else if (q[0] == 0 && q[1] && q[y]) {
number = 5;
}
else if (q[0] == 0 && q[1] && q[y] == 0) {
number = 6;
}
else if (q[0] == 0 && q[1] == 0 && q[y]) {
number = 7;
}
else if (q[0] == 0 && q[1] == 0 && q[y] == 0) {
number = "8";
}
else {
number = "error";
}
end();
}
function end() {
alert("the number you picked is: " + number + ".");
active = 0 //game is over
var again = confirm("play again?")
if (again) { // plays game again
history.go(0);
}
else {
window.close();
}
}
</script>
<center>
<br>
<h2> The Dichotomous Key</h2>
<br>
<br>
<br>
<br>
<form name = "answers">
<input type="button" name="yes" value="yes" onclick ="answer(1);">
<input type="button" name="no" value="no" onclick ="answer(0);">
</form>
<form name = "options">
<input type="button" name="start" value="start game" onclick ="history.go(0);">
</form>
<br>
<br>
<br>
<br>
<br>
<br>
<p>To play think of a number between 0 and 9 (1,2,3,4,5,6,7,8) and click the 'start game' button.
After answering 3 questions your number will be determined</p>
</center>
<script>
onLoad = start_game();
</script>
but i don't like the code in the function check(); as the if statements take up alot of space and is confusing when my friend (whom i am writing this for) wants to make more questions, results etc.
i came up with this:
<script>
var q = new Array()
q[0] = 1;
q[1] = 1;
q[2] = 1;
var result
function test() {
var a = q[0] + ";" + q[1] + ";" + q[2];
switch (result) {
case 0: a = "1,1,1";
break;
}
alert(result);
}
test();
</script>
but that doesn't work i get an undefined
i want to get rid of the if statements any suggestions???
<script>
var z;
var active = 1;
var x = 0;
var y = 2;
var number
var q = new Array();
q[0]
q[1]
q[2]
function start_game() {
alert ("think of a number between 0 and 9 and answer the following questions by clicking on the yes or no buttons");
questions();
}
function questions() {
var question_yes = new Array(); // yes questions
question_yes[0] = "Is the number less than 5?";
question_yes[1] = "Is the numbers square less than 5?";
question_yes[2] = "Is half the number NOT a whole number?";
question_yes[3] = "Is it even?";
var question_no = new Array(); // no questions
question_no[0] = "Is the number less than 5?";
question_no[1] = "Is half the number less than 3?";
question_no[2] = "Is it odd?";
question_no[3] = "Is it's square root a whole number?";
if (z) { // check if previous question was true
alert(question_yes[x])
}
else {
alert(question_no[x])
}
}
function answer(answer) {
if (active) {
z = answer;
q[x] = answer;
if (x == y) {
check();
}
else {
x++
questions();
}
}
else {
alert("to play click the 'start game' button");
}
}
function check() {
if (q[0] && q[1] && q[y]) {
number = 1;
}
else if (q[0] && q[1] && q[y] == 0) {
number = 2;
}
else if (q[0] && q[1] == 0 && q[y]) {
number = 3;
}
else if (q[0] && q[1] == 0 && q[y] == 0) {
number = 4;
}
else if (q[0] == 0 && q[1] && q[y]) {
number = 5;
}
else if (q[0] == 0 && q[1] && q[y] == 0) {
number = 6;
}
else if (q[0] == 0 && q[1] == 0 && q[y]) {
number = 7;
}
else if (q[0] == 0 && q[1] == 0 && q[y] == 0) {
number = "8";
}
else {
number = "error";
}
end();
}
function end() {
alert("the number you picked is: " + number + ".");
active = 0 //game is over
var again = confirm("play again?")
if (again) { // plays game again
history.go(0);
}
else {
window.close();
}
}
</script>
<center>
<br>
<h2> The Dichotomous Key</h2>
<br>
<br>
<br>
<br>
<form name = "answers">
<input type="button" name="yes" value="yes" onclick ="answer(1);">
<input type="button" name="no" value="no" onclick ="answer(0);">
</form>
<form name = "options">
<input type="button" name="start" value="start game" onclick ="history.go(0);">
</form>
<br>
<br>
<br>
<br>
<br>
<br>
<p>To play think of a number between 0 and 9 (1,2,3,4,5,6,7,8) and click the 'start game' button.
After answering 3 questions your number will be determined</p>
</center>
<script>
onLoad = start_game();
</script>
but i don't like the code in the function check(); as the if statements take up alot of space and is confusing when my friend (whom i am writing this for) wants to make more questions, results etc.
i came up with this:
<script>
var q = new Array()
q[0] = 1;
q[1] = 1;
q[2] = 1;
var result
function test() {
var a = q[0] + ";" + q[1] + ";" + q[2];
switch (result) {
case 0: a = "1,1,1";
break;
}
alert(result);
}
test();
</script>
but that doesn't work i get an undefined
i want to get rid of the if statements any suggestions???