Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:31
Show Gist options
  • Save anonymous/162a36bd1bdc6f95cd92f60f065c2a2c to your computer and use it in GitHub Desktop.
Save anonymous/162a36bd1bdc6f95cd92f60f065c2a2c to your computer and use it in GitHub Desktop.
https://repl.it/CQrh/92 created by sethopia
/*
SHIFT DECK
Create a function, deckShift() that takes a number and a direction ('left' or 'right) and returns an array of cards shifted that many spaces in that direction. This function should not mutate the original deck variable.
Examples
deckShift(4, 'left') => [6,7,8,9,10,'J','Q','A',2,3];
deckShift(2, 'right') => ['K','A',2,3,4,5,6,7,8,9,10,'J','Q'];
*/
var deck = [2,3,4,5,6,7,8,9,10, 'J', 'Q', 'K', 'A'];
var deckShift = function(index, direction) {
var shiftedCards = deck.slice();
if (direction === "left") {
for (var i = 0; i < index; i++) {
shiftedCards.push(shiftedCards.shift());
}
return shiftedCards;
} else if (direction === "right") {
for (var i = 0; i < index; i++) {
shiftedCards.unshift(shiftedCards.pop());
}
return shiftedCards;
} else {
return deck;
}
}
console.log(deckShift(4,"left"));
console.log(deckShift(2,"right"));
Native Browser JavaScript
>>> [ 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A', 2, 3, 4, 5 ]
[ 'K', 'A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment