I am trying to convert a nested for-loop with .map() to make it more functional. My goal to create a simple card deck. This is my working code:
generateDeck() {
const card = (suit, value) => {
return suit + value;
}
const suits = ["S", "H", "D", "C"];
const values = ["7", "8", "9", "10", "J", "D", "K", "A"];
for (let s = 0; s < suits.length; s++) {
for (let v = 0; v < values.length; v++) {
this.deck.push(card(suits[s], values[v]))
}
}
}
This is how far I came:
generateDeck() {
const card = (suit, value) => {
return suit + value;
}
const suits = ["S", "H", "D", "C"];
const values = ["7", "8", "9", "10", "J", "D", "K", "A"];
let deckNew = suits.map(s => {
values.forEach(v => { return s + v });
});
}
I can't get it to work. How would you avoid the nested for-loop in a clean more functional way?
Thanks!
map