Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param {number[]} A
- * @return {number[]}
- */
- var sortArrayByParityII = function(A) {
- //Declare length because argument will be mutated
- let length = A.length;
- //Declare oddsArray
- let oddsArray = [];
- //Declare evensArray
- let evensArray = [];
- //Declare count
- let count = 0;
- //Declare answerArray
- let answerArray = [];
- //Loop through array
- for (let i = 0; i < length; i += 1){
- //Conditional statement to check argument element if even or odd
- if (A[0] % 2 === 0) {
- //If even, shift argumentArray to evensArray
- evensArray.push(A.shift());
- } else {
- //else shift argumentArray to oddsArray
- oddsArray.push(A.shift());
- }
- }
- //Create while loop and execute until count equals length
- while(count !== length) {
- //Conditional statement when count modulo 2 is zero
- if (count % 2 === 0) {
- //Shift evensArray to answerArray if true and then increase count by 1
- answerArray.push(evensArray.shift());
- count += 1;
- } else {
- //Shift oddsArray to answerArray if false and then increase count by 1
- answerArray.push(oddsArray.shift());
- count += 1;
- }
- }
- //Return answerArray
- return answerArray
- };
- console.log(sortArrayByParityII([4, 2, 5, 7]));
Add Comment
Please, Sign In to add comment