- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning poker pair cards - JavaScript
We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly.
If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false.
For example: If the array is −
const arr = ['A', 'Q', '3', 'A', 'Q'];
Then our function should return −
'A' (as 'A' > 'Q' in card games)
Example
Following is the code −
const arr = ['A', 'Q', '3', 'A', 'Q']; const greatestPair = arr => { const legend = '23456789JQKA'; const pairs = []; for(let i = 0; i < arr.length; i++){ if(i !== arr.lastIndexOf(arr[i])){ pairs.push(arr[i]); }; }; if(!pairs.length){ return false; }; pairs.sort((a, b) => legend.indexOf(b) - legend.indexOf(a)); return pairs[0]; }; console.log(greatestPair(arr));
Output
Following is the output in the console −
A
- Related Articles
- Determining full house in poker - JavaScript
- How to Play Poker?
- Returning multiple values from a function using Tuple and Pair in C++
- Function returning another function in JavaScript
- Returning just greater array in JavaScript
- Rearranging cards into groups in JavaScript
- Accessing and returning nested array value - JavaScript?
- Returning values from a constructor in JavaScript?
- Returning the nth even number using JavaScript
- Returning number with increasing digits. in JavaScript
- Returning reverse array of integers using JavaScript
- Returning acronym based on a string in JavaScript
- Returning only odd number from array in JavaScript
- Returning lengthy words from a string using JavaScript
- Returning the highest number from object properties value – JavaScript

Advertisements