Home > Mobile >  Finding min and min with nested array
Finding min and min with nested array

Time:12-22

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

CodePudding user response:

Store the array, not just the number in min/max.

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 ]
]

let max = monthsNet[0];
let min = monthsNet[0];

/*
for (const item of monthsNet) {
  if (item[1] > max[1]) max = item;
  if (item[1] < min[1]) min = item;
}
*/

for (let i=1; i<monthsNet.length;   i) {
  const item = monthsNet[i];
  if (item[1] > max[1]) max = item;
  if (item[1] < min[1]) min = item;
}


console.log("Greatest Increase in Profits:", max)
console.log("Greatest Decrease in Profits:", min)

CodePudding user response:

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]
]

let { max, min } = monthsNet.reduce(({ max, min }, c) => {
    if (max[1] < c[1]) max = c;
    if (min[1] > c[1]) min = c;
    return { max, min }
}, { max: monthsNet[0], min: monthsNet[0] })
max = `Greatest Increase in Profits: ${max[0]} $ ${max[1]}`;
min = `Greatest Decrease in Profits: ${min[0]} $ ${min[1]}`;

console.log(max);
console.log(min);

  • Related