Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function deckOfCards(arr) {
- let result = []
- for (let card of arr) {
- let face = card.slice(0, -1);
- let suit = card.slice(-1);
- try {
- result.push(createCard(face, suit))
- } catch (err) {
- console.log(`Invalid card: ${card}`);
- return;
- }
- }
- console.log(result.join(' '));
- function createCard(face, suit) {
- let faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
- let suits = {
- "S": '\u2660',
- "H": '\u2665',
- "D": '\u2666',
- "C": '\u2663'
- }
- if (faces.includes(face) == false) {
- throw new Error(`Invalid Card: ${face}`)
- }
- if (Object.keys(suits).includes(suit) == false) {
- throw new Error(`Invalid Card: ${suit}`)
- }
- return {
- face,
- suit: suits[suit],
- toString() {
- return this.face + this.suit ;
- }
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement