mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:38:11 +00:00
LibJS: Add support for arbitrary arguments to Math.max
Address the FIXME in MathObject::max to handle an arbitrary number of arguments. Also adding a test case to verify the behavior of Math.max() while I'm here.
This commit is contained in:
parent
79539378c6
commit
4e54d0ff21
2 changed files with 22 additions and 13 deletions
|
@ -115,21 +115,16 @@ Value MathObject::max(Interpreter& interpreter)
|
|||
if (!interpreter.argument_count()) {
|
||||
// FIXME: I think this should return *negative* infinity.
|
||||
return js_infinity();
|
||||
}
|
||||
|
||||
if (interpreter.argument_count() == 1)
|
||||
} else if (interpreter.argument_count() == 1) {
|
||||
return interpreter.argument(0).to_number();
|
||||
|
||||
if (interpreter.argument_count() == 2) {
|
||||
auto a = interpreter.argument(0).to_number();
|
||||
auto b = interpreter.argument(1).to_number();
|
||||
return Value(a.as_double() > b.as_double() ? a : b);
|
||||
} else {
|
||||
Value max = interpreter.argument(0).to_number();
|
||||
for (size_t i = 1; i < interpreter.argument_count(); ++i) {
|
||||
Value cur = interpreter.argument(i).to_number();
|
||||
max = Value(cur.as_double() > max.as_double() ? cur : max);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
// FIXME: Support Math.max() with more arguments.
|
||||
ASSERT_NOT_REACHED();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue