summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Libraries/LibJS/Runtime/MathObject.cpp17
-rw-r--r--Libraries/LibJS/Runtime/MathObject.h1
-rw-r--r--Libraries/LibJS/Tests/Math.min.js12
3 files changed, 30 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp
index c9390abb74..cc46ae17fd 100644
--- a/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Libraries/LibJS/Runtime/MathObject.cpp
@@ -41,6 +41,7 @@ MathObject::MathObject()
put_native_function("ceil", ceil, 1);
put_native_function("round", round, 1);
put_native_function("max", max, 2);
+ put_native_function("min", min, 2);
put_native_function("trunc", trunc, 1);
put("E", Value(M_E));
@@ -139,6 +140,22 @@ Value MathObject::max(Interpreter& interpreter)
}
}
+Value MathObject::min(Interpreter& interpreter)
+{
+ if (!interpreter.argument_count())
+ return js_infinity();
+
+ if (interpreter.argument_count() == 1)
+ return interpreter.argument(0).to_number();
+
+ 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;
+}
+
Value MathObject::trunc(Interpreter& interpreter)
{
if (!interpreter.argument_count())
diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h
index e62e177c5f..3ab551d341 100644
--- a/Libraries/LibJS/Runtime/MathObject.h
+++ b/Libraries/LibJS/Runtime/MathObject.h
@@ -45,6 +45,7 @@ private:
static Value ceil(Interpreter&);
static Value round(Interpreter&);
static Value max(Interpreter&);
+ static Value min(Interpreter&);
static Value trunc(Interpreter&);
};
diff --git a/Libraries/LibJS/Tests/Math.min.js b/Libraries/LibJS/Tests/Math.min.js
new file mode 100644
index 0000000000..ed505995d4
--- /dev/null
+++ b/Libraries/LibJS/Tests/Math.min.js
@@ -0,0 +1,12 @@
+try {
+ assert(Math.min.length === 2);
+ assert(Math.min(1) === 1);
+ assert(Math.min(2, 1) === 1);
+ assert(Math.min(1, 2, 3) === 1);
+ assert(isNaN(Math.max(NaN)));
+ assert(isNaN(Math.max("String", 1)));
+
+ console.log("PASS");
+} catch {
+ console.log("FAIL");
+}