I've made changes that sum method ignores everything but numbers
Also I've refactored code to ES6
const sum = arr => { const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum return arr.reduce(reducer, 0); }
function sum(arr) {return arr.reduce((sum, elem) => sum + elem, 0);}- const sum = arr => {
- const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum
- return arr.reduce(reducer, 0);
- }
describe("Sum", function(){ it("should sum correctly", function(){ Test.assertEquals(sum([5,2,1]), 8, "Sum equals to 8"); }); it("should sum only numbers and skip the rest", function(){ Test.assertEquals(sum([5, 'a', 2, '$', 1]), 8, "Sum equals to 8"); Test.assertEquals(sum([5, '3', 2, '$', 1]), 8, "Sum equals to 8"); Test.assertEquals(sum([5, '3', 2, Infinity, 1]), Infinity, "Sum equals to Infinity"); }); });
- describe("Sum", function(){
- it("should sum correctly", function(){
Test.assertEquals(sum([5,2,1]), 8, "Sum equal to 8");- Test.assertEquals(sum([5,2,1]), 8, "Sum equals to 8");
- });
- it("should sum only numbers and skip the rest", function(){
- Test.assertEquals(sum([5, 'a', 2, '$', 1]), 8, "Sum equals to 8");
- Test.assertEquals(sum([5, '3', 2, '$', 1]), 8, "Sum equals to 8");
- Test.assertEquals(sum([5, '3', 2, Infinity, 1]), Infinity, "Sum equals to Infinity");
- });
- });