Created
February 18, 2022 20:03
-
-
Save saharshg29/f28c037b4dc402637b22f61a2fa0aa16 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
| let arra = [1, 4, 5, 6, 7] | |
| var duplicate = [] | |
| let o = [] | |
| let e = [] | |
| var on = 0; | |
| var en = 0; | |
| console.log("original array ", arra) | |
| const o2e = (arr) => { | |
| for (let i = 0; i < arr.length; i += 2) { | |
| o.push(arr[i]) | |
| } | |
| for (let i = 1; i < arr.length; i += 2) { | |
| e.push(arr[i]) | |
| } | |
| for (let jo = 0; jo < arr.length; jo++) { | |
| switch (jo % 2 == 0) { | |
| case true: | |
| for (let i = 0; i < o.length; i++) { | |
| duplicate.push(e[en]) | |
| break | |
| } | |
| en++ | |
| break | |
| case false: | |
| for (let i = 0; i < e.length; i++) { | |
| duplicate.push(o[on]) | |
| break; | |
| } | |
| on++ | |
| break | |
| default: | |
| } | |
| } | |
| console.log(duplicate) | |
| } | |
| if (arra.length % 2 == 0) { | |
| o2e(arra) | |
| } | |
| else { | |
| var pos = Math.floor(arra.length / 2) | |
| arra.splice(pos, 1) | |
| o2e(arra) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment