scrimba
Frontend Career Path
Working with APIs
Async JS
War - Determine the winning card part 1
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

War - Determine the winning card part 1
AboutCommentsNotes
War - Determine the winning card part 1
Expand for more info
index.js
run
preview
console
let deckId
const cardsContainer = document.getElementById("cards")
const newDeckBtn = document.getElementById("new-deck")
const drawCardBtn = document.getElementById("draw-cards")

function handleClick() {
fetch("https://apis.scrimba.com/deckofcards/api/deck/new/shuffle/")
.then(res => res.json())
.then(data => {
deckId = data.deck_id
})
}

newDeckBtn.addEventListener("click", handleClick)

drawCardBtn.addEventListener("click", () => {
fetch(`https://apis.scrimba.com/deckofcards/api/deck/${deckId}/draw/?count=2`)
.then(res => res.json())
.then(data => {
cardsContainer.children[0].innerHTML = `
<img src=${data.cards[0].image} class="card" />
`
cardsContainer.children[1].innerHTML = `
<img src=${data.cards[1].image} class="card" />
`
})
})
/**
* Challenge:
*
* Try to determine which of the 2 cards is the "winner" (has higher value)
* Aces are the card with the highest "score"
*
* In parts:
*
* 1. Create a function that takes 2 card objects as parameters,
* `card1` and `card2`. These card objects have a property called
* `value`, which can be any one of the following strings, in
* order of rising "score":
*
* "2", "3", "4", "5", "6", "7", "8", "9",
* "10", "JACK", "QUEEN", "KING", "ACE"
*
* I.e. "2" is the lowest score and "ACE" is the highest.
*
* The function should determine which of the 2 cards (`card1`
* or `card2`) has the higher score, or if they have the same score.
*
* Log which card wins (or "It's a tie!"
* if they're the same) to the console
*/
Console
{success:
true
, deck_id:
"5lexs5cu817g"
, remaining:
52
, shuffled:
true
}
,
[
{code:
"2D"
, image:
"https://deckofcardsapi.com/static/img/2D.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/2D.svg"
, png:
"https://deckofcardsapi.com/static/img/2D.png"
}
, value:
"2"
, suit:
"DIAMONDS"
}
,
{code:
"6C"
, image:
"https://deckofcardsapi.com/static/img/6C.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/6C.svg"
, png:
"https://deckofcardsapi.com/static/img/6C.png"
}
, value:
"6"
, suit:
"CLUBS"
}
]
,
[
{code:
"2H"
, image:
"https://deckofcardsapi.com/static/img/2H.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/2H.svg"
, png:
"https://deckofcardsapi.com/static/img/2H.png"
}
, value:
"2"
, suit:
"HEARTS"
}
,
{code:
"3H"
, image:
"https://deckofcardsapi.com/static/img/3H.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/3H.svg"
, png:
"https://deckofcardsapi.com/static/img/3H.png"
}
, value:
"3"
, suit:
"HEARTS"
}
]
,
[
{code:
"3C"
, image:
"https://deckofcardsapi.com/static/img/3C.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/3C.svg"
, png:
"https://deckofcardsapi.com/static/img/3C.png"
}
, value:
"3"
, suit:
"CLUBS"
}
,
{code:
"3S"
, image:
"https://deckofcardsapi.com/static/img/3S.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/3S.svg"
, png:
"https://deckofcardsapi.com/static/img/3S.png"
}
, value:
"3"
, suit:
"SPADES"
}
]
,
[
{code:
"7H"
, image:
"https://deckofcardsapi.com/static/img/7H.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/7H.svg"
, png:
"https://deckofcardsapi.com/static/img/7H.png"
}
, value:
"7"
, suit:
"HEARTS"
}
,
{code:
"JC"
, image:
"https://deckofcardsapi.com/static/img/JC.png"
, images:
{svg:
"https://deckofcardsapi.com/static/img/JC.svg"
, png:
"https://deckofcardsapi.com/static/img/JC.png"
}
, value:
"JACK"
, suit:
"CLUBS"
}
]
,
/index.html
-8:32