Fundamentals
Write a function add that uses rest parameters to sum all the numbers passed as arguments. The function should be able to handle any number of numerical inputs and return their total sum.
Examples:
add(1, 1); // returns 2
add(2, 3); // returns 5
add(-1, 1); // returns 0
add(-1, -1); // returns -2
add(1, 2, 3, 4); // returns 10
add(1, 0, -1); // returns 0
Constraints:
- The function should only accept numbers as arguments.
- You must use rest parameters (...) to handle the input arguments.
function add() { // Your arguments in brackets
// Your code here
}
const chai = require("chai");
const assert = chai.assert;
describe("Solution", function() {
it("should return the sum of two or more numbers", function() {
assert.strictEqual(add(1, 1), 2, "1 + 1 should equal 2");
assert.strictEqual(add(2, 3), 5, "2 + 3 should equal 5");
assert.strictEqual(add(-1, 1), 0, "-1 + 1 should equal 0");
assert.strictEqual(add(-1, -1), -2, "-1 + -1 should equal -2");
assert.strictEqual(add(1, 2, 3, 4), 10, "1 + 2 + 3 + 4 should equal 10");
assert.strictEqual(add(1, 0, -1), 0, "1 + 0 + -1 should equal 0");
});
});