The original challenge specified no low straights from the Ace, but it jarred with me and this routine is easily adjustable to support it. We just have to worry about sort orders during straights and to recognise the straight in the first place. The following updated version of getHandDetails() manages both of these:
constorder="23456789TJQKA"functiongetHandDetails(hand){constcards=hand.split("")constfaces=cards.map(a=>String.fromCharCode([77-order.indexOf(a[0])])).sort()constsuits=cards.map(a=>a[1]).sort()constcounts=faces.reduce(count,{})constduplicates=Object.values(counts).reduce(count,{})constflush=suits[0]===suits[4]constfirst=faces[0].charCodeAt(0)//Also handle low straightconstlowStraight=faces.join("")==="AJKLM"faces[0]=lowStraight?"N":faces[0]conststraight=lowStraight||faces.every((f,index)=>f.charCodeAt(0)-first===index)letrank=(flush&&straight&&1)||(duplicates[4]&&2)||(duplicates[3]&&duplicates[2]&&3)||(flush&&4)||(straight&&5)||(duplicates[3]&&6)||(duplicates[2]>1&&7)||(duplicates[2]&&8)||9return{rank,value:faces.sort(byCountFirst).join("")}functionbyCountFirst(a,b){//Counts are in reverse order - bigger is betterconstcountDiff=counts[b]-counts[a]if(countDiff)returncountDiff// If counts don't match returnreturnb>a?-1:b===a?0:1}functioncount(c,a){c[a]=(c[a]||0)+1returnc}}
We perform a simple string check for the low straight and then switch the order of 'A' to 'N' for aces in this circumstance.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The original challenge specified no low straights from the Ace, but it jarred with me and this routine is easily adjustable to support it. We just have to worry about sort orders during straights and to recognise the straight in the first place. The following updated version of getHandDetails() manages both of these:
We perform a simple string check for the low straight and then switch the order of 'A' to 'N' for aces in this circumstance.