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

What is Ruby on Rails and what is it good for?

Java Programming Interview Questions | MeritTrac

Azure SQL Database vs. Cosmos DB — Which Should You Choose?

CS371p Fall 2021: Week of Nov 15th- Nov 21st — William Crawford

DigitalOcean Sponsorship 2022

Mounting IBM Spectrum Protect as a drive letter on Windows

How to deploy a Laravel app to Heroku with GitLab CI/CD

7 Incredible Things You Can Do Using PHP In 2020

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

Interview questions 2022 basic javascript

Two Sum Problem (JavaScript)

Array Stepper Algorithm Problem

Creating a Singly Linked List in JavaScript.