For my application I am passing in a string of numbers via query string and would like to find the median of all the numbers. This task is pretty simple when working with actual numbers, but considering that I am trying to find the median of a string of numbers, I must convert the string into an array, sort it, convert the nums to integers etc. How could I achieve this result? Here is how I tried to achieve this:
let nums = req.query
let numArr = undefined
for(let x in nums) {
strX = nums[x].replace(/,/g, '').split('').sort();
numArr = strX.map(x => parseInt(x))
}
This method does not work for examples such as '5,7,9,1,12' because it sorts the array like this [1,1,2,5,7,9]
req.querya string of numbers (e.g.5,7,9,1,12)? 2. You are overwritingnumArrfor every number, which doesn't seem like what you want to do. Are you intending fornumArrto be the string of numbers converted into integers?