I'm wanting to find the min and max value of values inside a nested array, however the nested array has strings and numbers
I'm able to find min and max when using a variable of just the numbers but unable to find the min and max when it is mixed with a string, like such:
let monthsNet = [
[ 'Jan-2010', 116771 ], [ 'Feb-2010', -662642 ], [ 'Mar-2010', -391430 ],
[ 'Apr-2010', 379920 ], [ 'May-2010', 212354 ], [ 'Jun-2010', 510239 ],
[ 'Jul-2010', -428211 ], [ 'Aug-2010', -821271 ], [ 'Sep-2010', 693918 ],
[ 'Oct-2010', 416278 ], [ 'Nov-2010', -974163 ], [ 'Dec-2010', 860159 ],
[ 'Jan-2011', -1115009 ], [ 'Feb-2011', 1033048 ], [ 'Mar-2011', 95318 ],
[ 'Apr-2011', -308093 ], [ 'May-2011', 99052 ], [ 'Jun-2011', -521393 ],
[ 'Jul-2011', 605450 ], [ 'Aug-2011', 231727 ], [ 'Sep-2011', -65187 ],
[ 'Oct-2011', -702716 ], [ 'Nov-2011', 177975 ], [ 'Dec-2011', -1065544 ]
]
This is the code I used from just using a seperate variable with just numbers
let max = total[0];
let min = total[0];
for(i=0; i < total.length;i++){
if(total[i] >= max){
max = total[i]
}
if(total[i] <= min){
min = total[i]
}
}
console.log("Greatest Increase in Profits: $" + max)
console.log("Greatest Decrease in Profits: $" + min)
which returns
Greatest Increase in Profits: $ 1033048
Greatest Increase in Profits: $ -1115009
but I want it to return this instead the max and min with the corresponding month.
Greatest Increase in Profits: Feb-2011 $ 1033048
Greatest Increase in Profits: Jan-2011 $ -1115009