Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Logic Problem

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>
<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>
<body>
<p>
</body>
</html>

• Hi BDT,

Kill the last return

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

( •) (• )
>>V

• In the exmaple code given there are 9 elements to the array. I think you have an extra 1 in there.

Code:
`new Array(0,1,1,-1,1,0,-1,-1,0)`

• 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;}

• 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>
<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>
<body>
<p>
</body>
</html>

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•