-
-
Save nccharles/6e0ed873e3645841bf3451c2cda631de to your computer and use it in GitHub Desktop.
Write a mySort function which takes in an array integers, and should return an array of the inputed integers sorted such that the odd numbers come first and even numbers come last.
This file contains 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
/* | |
JavaScript | |
Write a mySort function which takes in an array integers, and should return an array of the inputed integers sorted such that the odd numbers come first and even numbers come last. | |
For exampl1e: | |
mySort( [90, 45, 66, 'bye', 100.5] ) | |
should return | |
[45, 66, 90, 100] | |
*/ | |
function mySort(nums) { | |
let evens = []; | |
let odds = []; | |
for (let i = 0; i < nums.length; i++) { | |
if(typeof nums[i] === "number"){ // ignore if its not a number | |
if ((nums[i] % 2) === 1) { | |
odds.push(parseInt(nums[i])); | |
} | |
else { | |
evens.push(parseInt(nums[i])); | |
} | |
} | |
} | |
let numsArray = odds.sort((a, b) => a - b).concat(evens.sort((a, b) => a - b)); | |
return numsArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment