Created
February 15, 2023 19:39
-
-
Save lubien/8cd5e4dbdfacb29e0c8184117c5488b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
/** | |
* Retrieve card from cards array at the 0-based position | |
* | |
* @param {number[]} cards | |
* @param {number} position | |
* | |
* @returns {number} the card | |
*/ | |
export function getItem(cards, position) { | |
return cards[position] | |
} | |
/** | |
* Exchange card with replacementCard at the 0-based position | |
* | |
* @param {number[]} cards | |
* @param {number} position | |
* @param {number} replacementCard | |
* | |
* @returns {number[]} the cards with the change applied | |
*/ | |
export function setItem(cards, position, replacementCard) { | |
cards.splice(position, 1, replacementCard) | |
return cards | |
} | |
/** | |
* Insert newCard at the end of the cards array | |
* | |
* @param {number[]} cards | |
* @param {number} newCard | |
* | |
* @returns {number[]} the cards with the newCard applied | |
*/ | |
export function insertItemAtTop(cards, newCard) { | |
cards.push(newCard) | |
return cards | |
} | |
/** | |
* Remove the card at the 0-based position | |
* | |
* @param {number[]} cards | |
* @param {number} position | |
* | |
* @returns {number[]} the cards without the removed card | |
*/ | |
export function removeItem(cards, position) { | |
cards.splice(position, 1) | |
return cards | |
} | |
/** | |
* Remove card from the end of the cards array | |
* | |
* @param {number[]} cards | |
* | |
* @returns {number[]} the cards without the removed card | |
*/ | |
export function removeItemFromTop(cards) { | |
cards.pop() | |
return cards | |
} | |
/** | |
* Insert newCard at beginning of the cards array | |
* | |
* @param {number[]} cards | |
* @param {number} newCard | |
* | |
* @returns {number[]} the cards including the new card | |
*/ | |
export function insertItemAtBottom(cards, newCard) { | |
cards.unshift(newCard) | |
return cards | |
} | |
/** | |
* Remove card from the beginning of the cards | |
* | |
* @param {number[]} cards | |
* | |
* @returns {number[]} the cards without the removed card | |
*/ | |
export function removeItemAtBottom(cards) { | |
cards.shift() | |
return cards | |
} | |
/** | |
* Compare the number of cards with the given stackSize | |
* | |
* @param {number[]} cards | |
* @param {number} stackSize | |
* | |
* @returns {boolean} true if there are exactly stackSize number of cards, false otherwise | |
*/ | |
export function checkSizeOfStack(cards, stackSize) { | |
return stackSize === cards.length | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment