Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions solutions/javascript/elyses-enchantments/1/enchantments.js
Original file line number Diff line number Diff line change
@@ -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;
}