Files
JS/06_js-debug/uebungen/u12_find-maximum-error/solution.js
Philippe Torrel ae9b04f27f
2026-08-19 11:41:58 +02:00

41 lines
992 B
JavaScript

// logical error
function findMaximum(a, b, c) {
if (a >= b && a >= c) {
return a;
} else if (b >= a && b >= c) {
return b;
} else {
return c;
}
}
const findMaximum2 = (...numbers) => {
return numbers.reduce((max, current) => {
if (current > max) return current;
else return max;
});
};
const findMaximum3 = (...numbers) => {
return Math.max(...numbers);
};
console.time('if else');
console.log(findMaximum(1, 2, 3)); // => 3
console.log(findMaximum(102, 59, 18)); // => 102
console.log(findMaximum(532, 532, 345)); // => 532
console.timeEnd('if else');
console.time('reduce');
console.log(findMaximum2(1, 2, 3)); // => 3
console.log(findMaximum2(102, 59, 18)); // => 102
console.log(findMaximum2(532, 532, 345)); // => 532
console.timeEnd('reduce');
console.time('Math.max');
console.log(findMaximum3(1, 2, 3)); // => 3
console.log(findMaximum3(102, 59, 18)); // => 102
console.log(findMaximum3(532, 532, 345)); // => 532
console.timeEnd('Math.max');