Last active
May 16, 2021 20:36
-
-
Save osahondev/059bfce710008997e15141a786a0eb7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
arrhas a single element. For example, if I ranshuffleClass([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.