Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active May 16, 2021 20:36
Show Gist options
  • Select an option

  • Save osahondev/059bfce710008997e15141a786a0eb7e to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/059bfce710008997e15141a786a0eb7e to your computer and use it in GitHub Desktop.
const shuffleClass = (arr,move) =>{
if( arr == undefined || arr == null ) return [];
if( arr.length == 0 || move == 0 || move == arr.length ) return arr;
if( Math.abs(move) > arr.length ){
move = move % arr.length; // to handle reshuffling
}
let newArr = [];
let start = 0;
let end = 0;
if( move < 0 ){
newArr = arr.slice( Math.abs(move),arr.length );
end = Math.abs(move);
}
else if( move > 0 ){
newArr = arr.slice( arr.length-move,arr.length);
end = arr.length-move;
}
while( start < end ){
newArr.push(arr[start] );
start++;
}
return newArr;
}
@meekg33k
Copy link

Hello @enaiho, thank you for participating in Week 6 of #AlgorithmFridays.

This is a very decent attempt at coming up with a solution for Apex College. Your solution is easy to read and passes most of the test cases. The one test case it doesn't pass however is:

  • When there is just one pupil. i.e. when arr has a single element. For example, if I ran shuffleClass([11], 5); // your solution returns [] instead of [11].

That might be something you want to check but apart from that, this was a really decent attempt, kudos to you!

Please let me know your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment