Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

too many if's 1

Status
Not open for further replies.

mattasouths

Programmer
Joined
Jan 27, 2006
Messages
6
Location
AU
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???

 
how about this?
Code:
<script>

var q = new Array()
q[0] = 1;
q[1] = 1;
q[2] = 1;

function test() {
	var result;
	var a = q[0] + ";" + q[1] + ";" + q[2];

	switch (a) {
		case "0;0;1":
			result = 1;
			break;
		case "0;1;1":
			result = 2;
			break;
		case "1;1;1":
			result = 3;
			break;
	}
	
	alert(result);
}

test();

</script>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Or to knock jeff's lines down a few more (kinda by cheating [lol])

Code:
<script>

var q = new Array()
q[0] = 1;
q[1] = 1;
q[2] = 1;

function test() {
    var a = q[0] + ";" + q[1] + ";" + q[2];
    var result = (a == "0;0;1") ? 1 : (a == "0;1;1") ? 2 : (a == "1;1;1") ? 3 : null;
    alert(result);
}

test();

</script>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
OP code said:
"Is half the number NOT a whole number?";

Why not just ask if the number is odd?


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Try this:

Code:
var number = 8;

if (q[0]) number -= 4;
if (q[1]) number -= 2;
if (q[2]) number -= 1;

return number;

Lee
 
jemminger thank you alot i will use your nice piece of code very easy to understand


and to lei

"Is half the number NOT a whole number?";

Why not just ask if the number is odd?

these questions don't matter they will be replaced

and to kaht your code is good but jemminger's is better to understand and will be more easier to edit in adding more reults and questions thanks anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top