...

View Full Version : Logic Problem



BDT
06-14-2003, 10:49 PM
Part of a javascript I am working on has an array of 9 numbers, each being a -1, 0 or 1. Although the array values will change during the use of the page an example would be:

direction = (0,1,1,1,-1,-1,-1,0,0)

I want to test the array to see if it meets the following condition: for any member of the array from 1..7 (forget 0 and 8), I want to find out if there is a 0 either 1 or 2 places to the right of a 1 or a zero 1 or 2 places to the left of a -1. If no zero meets the above conditions an alert box will open, but if there is any zero that 'passes the test', then the function will RETURN. Here is the applicable code, but it doesn't work right. The following array should satisfy the test (0,1,1,-1,0,-1,-1,0) because for position 2 (the third number) there is a 0 two places right of a 1. But it doesn't work and opens the alert box.

As always, I'm probably missing something pretty simple, but can someone point out the error of my ways??

thanks, BDT

<html>
<head>
<title>Jumping Game</title>
<script LANGUAGE="JavaScript" type="text/javascript">
var direction = new Array(0,1,1,-1,1,0,-1,-1,0)
testlose();
function testlose () {
if (direction[0]=="-1") {return;}
if (direction[8]=="1") {return;}
for (i=1;i<=7;i++) {
if (direction[i]=="1" && direction[i+1]=="0") {return;}
if (direction[i]=="1" && direction[i+2]=="0") {return;}
if (direction[i]=="-1" && direction[i-1]=="0") {return;}
if (direction[i]=="-1" && direction[i-2]=="0") {return;}

alert("i= " + i + " & direction[i]= " + direction[i] + " & direction[i+1]= " + direction[i+1] + " & direction[i+2]= " + direction[i+2]);
return;
}
}
</script>
</head>
<body>
<p>
</body>
</html>

Owl
06-15-2003, 02:21 AM
Hi BDT,

Kill the last return

:
:
direction[i+1] + " & direction[i+2]= " + direction[i+2]);
return;
}
}

( ) ( )
>>V

Adam20002
06-15-2003, 10:31 PM
In the exmaple code given there are 9 elements to the array. I think you have an extra 1 in there.




new Array(0,1,1,-1,1,0,-1,-1,0)

glenngv
06-16-2003, 02:48 AM
The following array should satisfy the test (0,1,1,-1,0,-1,-1,0) because for position 2 (the third number) there is a 0 two places right of a 1. But it doesn't work and opens the alert box.


taking the above statement as the reference, I think you need to add 3 (not 2) in the direction when you say 2 places to the right and add 2 (not 1) when you say 1 place to the right.

if (direction[i]=="1" && direction[i+2]=="0") {return;}
if (direction[i]=="1" && direction[i+3]=="0") {return;}
if (direction[i]=="-1" && direction[i-2]=="0") {return;}
if (direction[i]=="-1" && direction[i-3]=="0") {return;}

BDT
06-16-2003, 04:22 PM
Thanks everyone. I think that your comments gave me what I need to fix my problem.

Yep, the return has to go ... at least to the outside of the if statement. Likewise the alert goes there too, so that it only get used if there is not even one case where the condition is met. And I did omit an item from the Array ... I meant to use
(0,1,1,-1,1,0,-1,-1,0). Now I don't get an alert with this Array, but I do with (0,1,1,1,-1,-1,-1,0,0) ... just as it should be.

thanks again, BDT

<html>
<head>
<title>Jumping Game</title>
<script LANGUAGE="JavaScript" type="text/javascript">
var direction = new Array(0,1,1,-1,1,0,-1,-1,0)
testlose();
function testlose () {
if (direction[0]=="-1") {return;}
if (direction[8]=="1") {return;}
for (i=1;i<=7;i++) {
if (direction[i]=="1" && direction[i+1]=="0") {return;}
if (direction[i]=="1" && direction[i+2]=="0") {return;}
if (direction[i]=="-1" && direction[i-1]=="0") {return;}
if (direction[i]=="-1" && direction[i-2]=="0") {return;}
}
alert("i= " + i + " & direction[i]= " + direction[i] + " & direction[i+1]= " + direction[i+1] + " & direction[i+2]= " + direction[i+2]);
return;
}
</script>
</head>
<body>
<p>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum