javascript - Alternate structure for a sequence of If statements? -


i'm programming poker program in javascript. have hand class has properties "cards", "value" , "valuecards". value property integer corresponds hand type, , valuecards array of 5 cards corresponds hand type. example, if original 7 cards(contained in cards property) contains flush, this.value flip 6, , this.valuecards equal 5 cards equal highest flush.

i have 1 method each hand type, , of them change value , valuecards if hand type detected. have accessor method value called getvalue, when went make method run tests on hand , keep highest one, came out looking this:

poker.hand.prototype.gettruevalue = function () {     this.teststraightflush();     if(this.value == poker.hand_type.straight_flush){ return; }      this.testquads();     if(this.value == poker.hand_type.quads){ return; }      this.testfullhouse();     if(this.value == poker.hand_type.full_house){ return; }      this.testflush();     if(this.value == poker.hand_type.flush){ return; }      this.teststraight();     if(this.value == poker.hand_type.straight){ return; }      this.testtrips();     if(this.value == poker.hand_type.trips){ return; }      this.testtwopair();     if(this.value == poker.hand_type.two_pair){ return; }      this.testpair();     if(this.value == poker.hand_type.pair){ return; }      this.gethighcards(); }; 

i mean, method works fine. bothers me, maybe should doing different way. go against convention?

if change this.test* functions return true if "hand" found, or return false if not - ugly, yet somehow satisfying, as

poker.hand.prototype.gettruevalue = function () {     this.teststraightflush() ||     this.testquads() ||     this.testfullhouse() ||     this.testflush() ||     this.teststraight() ||     this.testtrips() ||     this.testtwopair() ||     this.testpair() ||     this.gethighcards(); }; 

or

change this.test* functions check if this.found false, , set this.found = true if hand found, you'd simply

poker.hand.prototype.gettruevalue = function () {     this.found = false;     this.teststraightflush();     this.testquads();     this.testfullhouse();     this.testflush();     this.teststraight();     this.testtrips();     this.testtwopair();     this.testpair();     this.gethighcards(); }; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -