Create a function to be able to add, substract, multiply or divide all the numbers given in the array provided.
Parameters:
1. Array
2. Operator:
-'Add'
-'Substract'
-'Divide'
-'Multiply'
Example:
function: getResult(array, operator)
array: [4, 2, 1]
operator: 'Add'
result: 7
operator: 'Substract'
result: 1
operator: 'Divide'
result: 2
operator: 'Multiply'
result: 8
function getResult(array, operator) {
//Good luck!
}
const chai = require("chai");
const assert = chai.assert;
describe("Result:", function() {
//Zero
let test1 = [0, 1, 2],
test2 = [1, 0, 2],
test3 = [1, 2, 0],
//Negatives
test4 = [-1, 1, 2],
test5 = [1, -1, 2],
test6 = [1, 2, -1],
//String
test7 = ['a', 1, 2],
test8 = [1, 'a', 2],
test9 = [1, 2, 'a'],
//Empty String
test10 = ['', 1, 2],
test11 = [1, '', 2],
test12 = [1, 2, ''],
//Null
test13 = [null, 1, 2],
test14 = [1, null, 2],
test15 = [1, 2, null],
//Large array
test16 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
//Empty array
test17 = [],
//Null
test18 = null,
//Empty String
test19 = '',
//All same
test20 = ['', '', ''],
test21 = ['a', 'a', 'a'],
test22 = [null, null, null],
test23 = [0, 0, 0],
test24 = [-1, -1, -1],
test25 = ['', '', ''];
let res1 = getResult(test1, 'Add'),
res2 = getResult(test1, 'Substract');
res3 = getResult(test1, 'Multiply');
res4 = getResult(test1, 'Divide');
res5 = getResult(test2, 'Add');
res6 = getResult(test2, 'Substract');
res7 = getResult(test2, 'Multiply');
res8 = getResult(test2, 'Divide');
res9 = getResult(test3, 'Add');
res10 = getResult(test3, 'Substract');
res11 = getResult(test3, 'Multiply');
res12 = getResult(test3, 'Divide');
res13 = getResult(test4, 'Add');
res14 = getResult(test4, 'Substract');
res15 = getResult(test4, 'Multiply');
res16 = getResult(test4, 'Divide');
res17 = getResult(test5, 'Add');
res18 = getResult(test5, 'Substract');
res19 = getResult(test5, 'Multiply');
res20 = getResult(test5, 'Divide');
res21 = getResult(test6, 'Add');
res22 = getResult(test6, 'Substract');
res23 = getResult(test6, 'Multiply');
res24 = getResult(test6, 'Divide');
res25 = getResult(test7, 'Add');
res26 = getResult(test7, 'Substract');
res27 = getResult(test7, 'Multiply');
res28 = getResult(test7, 'Divide');
res29 = getResult(test8, 'Add');
res30 = getResult(test8, 'Substract');
res31 = getResult(test8, 'Multiply');
res32 = getResult(test8, 'Divide');
res33 = getResult(test9, 'Add');
res34 = getResult(test9, 'Substract');
res35 = getResult(test9, 'Multiply');
res36 = getResult(test9, 'Divide');
res37 = getResult(test10, 'Add');
res38 = getResult(test10, 'Substract');
res39 = getResult(test10, 'Multiply');
res40 = getResult(test10, 'Divide');
res41 = getResult(test11, 'Add');
res42 = getResult(test11, 'Substract');
res43 = getResult(test11, 'Multiply');
res44 = getResult(test11, 'Divide');
res45 = getResult(test12, 'Add');
res46 = getResult(test12, 'Substract');
res47 = getResult(test12, 'Multiply');
res48 = getResult(test12, 'Divide');
res49 = getResult(test13, 'Add');
res50 = getResult(test13, 'Substract');
res51 = getResult(test13, 'Multiply');
res52 = getResult(test13, 'Divide');
res53 = getResult(test14, 'Add');
res54 = getResult(test14, 'Substract');
res55 = getResult(test14, 'Multiply');
res56 = getResult(test14, 'Divide');
res57 = getResult(test15, 'Add');
res58 = getResult(test15, 'Substract');
res59 = getResult(test15, 'Multiply');
res60 = getResult(test15, 'Divide');
res61 = getResult(test16, 'Add');
res62 = getResult(test16, 'Substract');
res63 = getResult(test16, 'Multiply');
res64 = getResult(test16, 'Divide');
res65 = getResult(test17, 'Add');
res66 = getResult(test17, 'Substract');
res67 = getResult(test17, 'Multiply');
res68 = getResult(test17, 'Divide');
res69 = getResult(test18, 'Add');
res70 = getResult(test18, 'Substract');
res71 = getResult(test18, 'Multiply');
res72 = getResult(test18, 'Divide');
res73 = getResult(test19, 'Add');
res74 = getResult(test19, 'Substract');
res75 = getResult(test19, 'Multiply');
res76 = getResult(test19, 'Divide');
res77 = getResult(test20, 'Add');
res78 = getResult(test20, 'Substract');
res79 = getResult(test20, 'Multiply');
res80 = getResult(test20, 'Divide');
res81 = getResult(test21, 'Add');
res82 = getResult(test21, 'Substract');
res83 = getResult(test21, 'Multiply');
res84 = getResult(test21, 'Divide');
res85 = getResult(test22, 'Add');
res86 = getResult(test22, 'Substract');
res87 = getResult(test22, 'Multiply');
res88 = getResult(test22, 'Divide');
res89 = getResult(test23, 'Add');
res90 = getResult(test23, 'Substract');
res91 = getResult(test23, 'Multiply');
res92 = getResult(test23, 'Divide');
res93 = getResult(test24, 'Add');
res94 = getResult(test24, 'Substract');
res95 = getResult(test24, 'Multiply');
res96 = getResult(test24, 'Divide');
res97 = getResult(test25, 'Add');
res98 = getResult(test25, 'Substract');
res99 = getResult(test25, 'Multiply');
res100 = getResult(test25, 'Divide');
it("Test", function() {
console.log('Add the array: ', test1);
assert.equal(res1, 3);
});
it("Test", function() {
console.log('Substract the array: ', test1);
assert.equal(res2, -3);
});
it("Test", function() {
console.log('Multiply the array: ', test1);
assert.equal(res3, 0);
});
it("Test", function() {
console.log('Divide the array: ', test1);
assert.equal(res4, 0);
});
it("Test", function() {
console.log('Add the array: ', test2);
assert.equal(res5, 3);
});
it("Test", function() {
console.log('Substract the array: ', test2);
assert.equal(res6, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test2);
assert.equal(res7, 0);
});
it("Test", function() {
console.log('Divide the array: ', test2);
assert.equal(res8, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test3);
assert.equal(res9, 3);
});
it("Test", function() {
console.log('Substract the array: ', test3);
assert.equal(res10, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test3);
assert.equal(res11, 0);
});
it("Test", function() {
console.log('Divide the array: ', test3);
assert.equal(res12, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test4);
assert.equal(res13, 2);
});
it("Test", function() {
console.log('Substract the array: ', test4);
assert.equal(res14, -4);
});
it("Test", function() {
console.log('Multiply the array: ', test4);
assert.equal(res15, -2);
});
it("Test", function() {
console.log('Divide the array: ', test4);
assert.equal(res16, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test5);
assert.equal(res17, 2);
});
it("Test", function() {
console.log('Substract the array: ', test5);
assert.equal(res18, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test5);
assert.equal(res19, -2);
});
it("Test", function() {
console.log('Divide the array: ', test5);
assert.equal(res20, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test6);
assert.equal(res21, 2);
});
it("Test", function() {
console.log('Substract the array: ', test6);
assert.equal(res22, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test6);
assert.equal(res23, -2);
});
it("Test", function() {
console.log('Divide the array: ', test6);
assert.equal(res24, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test7);
assert.equal(res25, 3);
});
it("Test", function() {
console.log('Substract the array: ', test7);
assert.equal(res26, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test7);
assert.equal(res27, 2);
});
it("Test", function() {
console.log('Divide the array: ', test7);
assert.equal(res28, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test8);
assert.equal(res29, 3);
});
it("Test", function() {
console.log('Substract the array: ', test8);
assert.equal(res30, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test8);
assert.equal(res31, 2);
});
it("Test", function() {
console.log('Divide the array: ', test8);
assert.equal(res32, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test9);
assert.equal(res33, 3);
});
it("Test", function() {
console.log('Substract the array: ', test9);
assert.equal(res34, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test9);
assert.equal(res35, 2);
});
it("Test", function() {
console.log('Divide the array: ', test9);
assert.equal(res36, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test10);
assert.equal(res37, 3);
});
it("Test", function() {
console.log('Substract the array: ', test10);
assert.equal(res38, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test10);
assert.equal(res39, 2);
});
it("Test", function() {
console.log('Divide the array: ', test10);
assert.equal(res40, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test11);
assert.equal(res41, 3);
});
it("Test", function() {
console.log('Substract the array: ', test11);
assert.equal(res42, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test11);
assert.equal(res43, 2);
});
it("Test", function() {
console.log('Divide the array: ', test11);
assert.equal(res44, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test12);
assert.equal(res45, 3);
});
it("Test", function() {
console.log('Substract the array: ', test12);
assert.equal(res46, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test12);
assert.equal(res47, 2);
});
it("Test", function() {
console.log('Divide the array: ', test12);
assert.equal(res48, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test13);
assert.equal(res49, 3);
});
it("Test", function() {
console.log('Substract the array: ', test13);
assert.equal(res50, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test13);
assert.equal(res51, 2);
});
it("Test", function() {
console.log('Divide the array: ', test13);
assert.equal(res52, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test14);
assert.equal(res53, 3);
});
it("Test", function() {
console.log('Substract the array: ', test14);
assert.equal(res54, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test14);
assert.equal(res55, 2);
});
it("Test", function() {
console.log('Divide the array: ', test14);
assert.equal(res56, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test15);
assert.equal(res57, 3);
});
it("Test", function() {
console.log('Substract the array: ', test15);
assert.equal(res58, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test15);
assert.equal(res59, 2);
});
it("Test", function() {
console.log('Divide the array: ', test15);
assert.equal(res60, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test16);
assert.equal(res61, 465);
});
it("Test", function() {
console.log('Substract the array: ', test16);
assert.equal(res62, -463);
});
it("Test", function() {
console.log('Multiply the array: ', test16);
assert.equal(res63, 2.6525285981219103e+32);
});
it("Test", function() {
console.log('Divide the array: ', test16);
assert.equal(res64, 3.769987628815906e-33);
});
it("Test", function() {
console.log('Add the array: ', test17);
assert.equal(res65, 0);
});
it("Test", function() {
console.log('Substract the array: ', test17);
assert.equal(res66, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test17);
assert.equal(res67, 0);
});
it("Test", function() {
console.log('Divide the array: ', test17);
assert.equal(res68, 0);
});
it("Test", function() {
console.log('Add the array: ', test18);
assert.equal(res69, 0);
});
it("Test", function() {
console.log('Substract the array: ', test18);
assert.equal(res70, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test18);
assert.equal(res71, 0);
});
it("Test", function() {
console.log('Divide the array: ', test18);
assert.equal(res72, 0);
});
it("Test", function() {
console.log('Add the array: ', test19);
assert.equal(res73, 0);
});
it("Test", function() {
console.log('Substract the array: ', test19);
assert.equal(res74, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test19);
assert.equal(res75, 0);
});
it("Test", function() {
console.log('Divide the array: ', test19);
assert.equal(res76, 0);
});
it("Test", function() {
console.log('Add the array: ', test20);
assert.equal(res77, 0);
});
it("Test", function() {
console.log('Substract the array: ', test20);
assert.equal(res78, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test20);
assert.equal(res79, 0);
});
it("Test", function() {
console.log('Divide the array: ', test20);
assert.equal(res80, 0);
});
it("Test", function() {
console.log('Add the array: ', test21);
assert.equal(res81, 0);
});
it("Test", function() {
console.log('Substract the array: ', test21);
assert.equal(res82, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test21);
assert.equal(res83, 0);
});
it("Test", function() {
console.log('Divide the array: ', test21);
assert.equal(res84, 0);
});
it("Test", function() {
console.log('Add the array: ', test22);
assert.equal(res85, 0);
});
it("Test", function() {
console.log('Substract the array: ', test22);
assert.equal(res86, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test22);
assert.equal(res87, 0);
});
it("Test", function() {
console.log('Divide the array: ', test22);
assert.equal(res88, 0);
});
it("Test", function() {
console.log('Add the array: ', test23);
assert.equal(res89, 0);
});
it("Test", function() {
console.log('Substract the array: ', test23);
assert.equal(res90, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test23);
assert.equal(res91, 0);
});
it("Test", function() {
console.log('Divide the array: ', test23);
assert.equal(res92, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test24);
assert.equal(res93, -3);
});
it("Test", function() {
console.log('Substract the array: ', test24);
assert.equal(res94, 1);
});
it("Test", function() {
console.log('Multiply the array: ', test24);
assert.equal(res95, -1);
});
it("Test", function() {
console.log('Divide the array: ', test24);
assert.equal(res96, -1);
});
it("Test", function() {
console.log('Add the array: ', test25);
assert.equal(res97, 0);
});
it("Test", function() {
console.log('Substract the array: ', test25);
assert.equal(res98, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test25);
assert.equal(res99, 0);
});
it("Test", function() {
console.log('Divide the array: ', test25);
assert.equal(res100, 0);
});
});