I have an array that I need to the following with:
- arranged from lowest to highest.
- Select the middle two numbers.
- Sum the middle two scores.
Am I on the right track?
var numArray = [2, 1, 3, 1, 4, 2];
numArray.sort((a, b) => a - b);
var sum = numArray[2] + numArray[3];
I think Array.reduce can be used somehow?
var sum = numArray[2] + numArray[3]; gives me two numebrs together and not their sum.
How do I do math function sum rather than combined two variables into one?
Edit: DrafsApp for Mac was adding two values "2" and "2" into "22"
I had to change the code to this and it works:
var sum = Number(numArray[2]) + Number(numArray[3]);