0

I'm still at my card game. Again and again I have such places as now where I can't get any further. I have two hands with cards. I want to output the names and suits as an array. But that only works with a two-digit combination. As soon as there is a 10 it is three digits and it no longer works.

'use strict'

let hand1 = ['7H', '2H', 'QH', '9H', '5H']
let hand2 = ['7H', '2H', 'QH', '10H', '5H']

let toSuit = hand => hand.map(a => a[0])
let toName = hand => hand.map(a => a[1])



console.log(toName(hand1))
console.log(toSuit(hand1))

console.log(toName(hand2))
console.log(toSuit(hand2))

I can do that for the single card but not for the output as an array.

'use strict'
    
let card1 = '9H'
let card2 = '10H'
  
let toSuit1 = card => card.charAt(card.length - 1)
let toName1 = card => card.slice(0,-1)  
   
console.log(toSuit1(card1))
console.log(toSuit1(card2))

console.log(toName1(card1))
console.log(toName1(card2))

Thanks @blex

'use strict'

let hand1 = ['7H', '2H', 'QH', '9H', '5H']
let hand2 = ['7H', '2H', 'QH', '10H', '5H']

let toSuit1 = card => card.charAt(card.length - 1);
let toName1 = card => card.slice(0,-1); 

let toSuit = hand => hand.map(toSuit1)
let toName = hand => hand.map(toName1)

console.log(toName(hand1))
console.log(toSuit(hand1))

console.log(toName(hand2))
console.log(toSuit(hand2))
2
  • You have all the pieces, now join them together: toSuit = hand => hand.map(toSuit1); toName = hand => hand.map(toName1); Commented Mar 21, 2021 at 10:42
  • @blex I've been sitting on the problem for more than an hour. Simply together. LOL. I was only focused on a [0]. I find it really difficult to see these simple things all the time. Thank you. Commented Mar 21, 2021 at 11:21

1 Answer 1

2

You could just take the functions for mapping.

'use strict';

const
    toSuit = card => card.charAt(card.length - 1),
    toName = card => card.slice(0,-1),
    toSuits = hand => hand.map(toSuit),
    toNames = hand => hand.map(toName),

    hand1 = ['7H', '2H', 'QH', '9H', '5H'],
    hand2 = ['7H', '2H', 'QH', '10H', '5H'];

console.log(...toNames(hand1))
console.log(...toSuits(hand1))

console.log(...toNames(hand2))
console.log(...toSuits(hand2))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.