This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// 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');
|
||||
|
||||
Reference in New Issue
Block a user