Welcome to WebmasterWorld Guest from 54.196.17.193

Forum Moderators: open

Message Too Old, No Replies

Loop with number

for card game

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

Preferred Member

10+ Year Member

joined:Nov 12, 2004
posts:550
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


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)

Preferred Member

10+ Year Member

joined:Nov 12, 2004
posts:550
votes: 0


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

Preferred Member

10+ Year Member

joined:Nov 12, 2004
posts:550
votes: 0


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 June 10, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


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 June 10, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 12, 2004
posts:550
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members