Solving Popular Algorithms: Shuffle the Array

A good boy doing a good shuffle
nums = [2,5,1,3,4,7], n = 3
[2,3,5,4,1,7] 
let partOne = []for (let i = 0; i < nums.length/2; i++) {
partOne.push(nums[i])
}
let partTwo = []for (let j= n; j < nums.length; j++) {
partTwo.push(nums[j])
}
let finalArr = []while (partOne.length > 0 && partTwo.length > 0) {
finalArr.push(partOne.shift(), partTwo.shift()
}
shuffle = (nums, n) => {
let partOne = []
for (let i = 0; i < nums.length/2; i++) {
partOne.push(nums[i])
}
let partTwo = []
for (let j= n; j < nums.length; j++) {
partTwo.push(nums[j])
}
let finalArr = []
while (partOne.length > 0 && partTwo.length > 0) {
finalArr.push(partOne.shift(), partTwo.shift()
}
return finalArr
}
Happy dancing for two reasons: 1) I solved this problem and 2) I won another game in my fantasy league!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

TeraBox 1TB of free storage in the cloud

Spacecrafts With Explosive Engines: How to Survive a Fight With Technical Debt

3 Tips For Maintaining Your Scala Projects

Oracle GoldenGate MySQL to MySQL Configuration part 1

SolFarm v1

Install Drush locally on Windows

Bloggers about us!

Understanding The Box Model in CSS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HopeGiometti

HopeGiometti

More from Medium

Programming a Queue with a Doubly Linked List

Algorithms Explained Fast and Clear (No BS)

The First Project

CodeSignal Solutions — Arrays