Welcome to WebmasterWorld Guest from 23.23.46.20

Forum Moderators: open

Loop with number

for card game

   
3:12 am on May 29, 2009 (gmt 0)

10+ Year Member



I am creating a simple local card game and stalled with one function.

I am trying to check an array of numbers to see if it's "straight", eg. 789 or 34567.

for (i=0; i<cardArray.length; i++){

var s=Number(cardArray[i+1])-Number(cardArray[i]);

if(Number(s)==1){
var cardCheck="valid";

}else{
var cardCheck="notvalid";

}

}

The above code sometimes work sometimes not. Appreciate your help.. Thanks

3:48 am on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



As cardCheck is set each time, the final value depends only on last 2 cards.

Use innocent until proved otherwise method.

var cardCheck="valid";
for (var i=0; i<(cardArray.length-1); i++){
var s=Number(cardArray[i+1])-Number(cardArray[i]);
if(s!==1){cardCheck="notvalid";}
}

You will need to also that there are at least 2 cards

1:18 am on May 30, 2009 (gmt 0)

10+ Year Member



That's perpect. Thanks a lot.
11:37 am on Jun 10, 2009 (gmt 0)

10+ Year Member



I also want to check the card suits if all are the same. I have an array of cards suits and would like to find out if all the values are the same. Thanks a lot for the help.
12:04 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Much the same

var suiteCheck="valid";
for (var i=0; i<(suiteArray.length-1); i++){
if( suiteArray[i+1] !== suiteArray[i] ){suiteCheck="notvalid";}
}

Regards

12:29 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Thanks a lot really. I missed the -1 part. Thanks again.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month