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