If you don’t want to use the Math.min and Math.max functions, I provided an altenartive solution. We basically just merge all 3 arrays into one and only keep the common values. hen we find the min and max manually by iterating through the array.
const arrayB = [1, 2.2, 7, 3, 3.2, 2.7, 6.2, 1.9, 3.3, 2, 9]
const arrayC = [2.7, 3, 3.3, 8, 2.7, 5, 1.5, 4]
//we combine the arrays and only keep the common values
const commonValues = [arrayA, arrayB, arrayC].reduce((a, b) => a.filter(v =>
b.includes(v)));
let min = Number.MAX_VALUE
let max = Number.MIN_VALUE
for (let i = 0; i < commonValues.length; i++) {
if (min > commonValues[i]) {
min = commonValues[i]
}
if (max < commonValues[i]) {
max = commonValues[i]
}
}
console.log(min)
console.log(max)
3
Answers
You could take the common values and get min and max value from this set.
Lodash version, using
_.intersection
_.min
and_.max
:If you don’t want to use the Math.min and Math.max functions, I provided an altenartive solution. We basically just merge all 3 arrays into one and only keep the common values. hen we find the min and max manually by iterating through the array.