homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Loop with number
for card game
thegreatpretender




msg:3922172
 3:12 am on May 29, 2009 (gmt 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

 

daveVk




msg:3922182
 3:48 am on May 29, 2009 (gmt 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

thegreatpretender




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

That's perpect. Thanks a lot.

thegreatpretender




msg:3930331
 11:37 am on Jun 10, 2009 (gmt 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.

daveVk




msg:3930340
 12:04 pm on Jun 10, 2009 (gmt 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

thegreatpretender




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved