diff --git a/solutions/javascript/elyses-enchantments/1/enchantments.js b/solutions/javascript/elyses-enchantments/1/enchantments.js new file mode 100644 index 0000000..53c05f3 --- /dev/null +++ b/solutions/javascript/elyses-enchantments/1/enchantments.js @@ -0,0 +1,120 @@ +// @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) { + if(position >= 0 && position < cards.length){ + return cards[position]; +}else { + return "Number is out of range "; +} +} + +/** + * 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) { + if (position >= 0 && position < cards.length){ + cards[position] = replacementCard; + return cards; + } + else{ + return "out of the range"; + } +} + +/** + * 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) { + if (position >= 0 && position < cards.length){ + cards.splice(position,1); + return cards; +}else{ + return "Position is out range"; +} +} + +/** + * 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) { + if(cards.length > 0){ + cards.shift(); + return cards; + } + else{ + return " cards is empty" + } +} + +/** + * 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 cards.length === stackSize; +}