Determining full house in poker - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

   The “full house in poker” is a situation when a player, out of their five cards, has at least three cards identical. We are required to write a JavaScript function that takes in an array of five elements representing a card each and returns true if there's a full house situation, false otherwise.

Example

Following is the code −

const arr2 = ['K', '2', 'K', 'A', 'J'];
const isFullHouse = arr => {
   const copy = arr.slice();
   for(let i = 0; i < arr.length; ){
      const el = copy.splice(i, 1)[0];
      if(copy.includes(el)){
         copy.splice(copy.indexOf(el), 1);
         if(copy.includes(el)){
            return true;
         }
      }else{
         i++;
      }
   };
   return false;
};
console.log(isFullHouse(arr1));
console.log(isFullHouse(arr2));

Output

Following is the output in the console −

true
false
raja
Published on 18-Sep-2020 09:35:54
Advertisements