Guest User

Untitled

a guest
Feb 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /**
  2. * @param {number[]} A
  3. * @return {number[]}
  4. */
  5. var sortArrayByParityII = function(A) {
  6. //Declare length because argument will be mutated
  7. let length = A.length;
  8. //Declare oddsArray
  9. let oddsArray = [];
  10. //Declare evensArray
  11. let evensArray = [];
  12. //Declare count
  13. let count = 0;
  14. //Declare answerArray
  15. let answerArray = [];
  16.  
  17. //Loop through array
  18. for (let i = 0; i < length; i += 1){
  19. //Conditional statement to check argument element if even or odd
  20. if (A[0] % 2 === 0) {
  21. //If even, shift argumentArray to evensArray
  22. evensArray.push(A.shift());
  23. } else {
  24. //else shift argumentArray to oddsArray
  25. oddsArray.push(A.shift());
  26. }
  27. }
  28.  
  29. //Create while loop and execute until count equals length
  30. while(count !== length) {
  31. //Conditional statement when count modulo 2 is zero
  32. if (count % 2 === 0) {
  33. //Shift evensArray to answerArray if true and then increase count by 1
  34. answerArray.push(evensArray.shift());
  35. count += 1;
  36. } else {
  37. //Shift oddsArray to answerArray if false and then increase count by 1
  38. answerArray.push(oddsArray.shift());
  39. count += 1;
  40. }
  41. }
  42. //Return answerArray
  43. return answerArray
  44. };
  45.  
  46. console.log(sortArrayByParityII([4, 2, 5, 7]));
Add Comment
Please, Sign In to add comment