/* Array.reduce( * (accumulator, currentValue, currentIndex, array) => { * return accumulator + currentValue; * }, * initialValue * ); */ function sumOfElements(arr) { return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0; }
- /* Array.reduce(
- * (accumulator, currentValue, currentIndex, array) => {
- * return accumulator + currentValue;
- * },
- * initialValue
- * );
- */
function sumOfElements(arr) {if(arguments.length === 0)arr = [1,2,3,4,5];var total = arr.reduce( (sum, currentValue) => {return sum + currentValue}, 0 );console.log(total);return total;- function sumOfElements(arr) {
- return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0;
- }
// TODO: Replace examples and use TDD development by writing your own tests // These are some CW specific test methods available: // Test.expect(boolean, [optional] message) // Test.assertEquals(actual, expected, [optional] message) // Test.assertSimilar(actual, expected, [optional] message) // Test.assertNotEquals(actual, expected, [optional] message) // NodeJS assert is also automatically required for you. // assert(true) // assert.strictEqual({a: 1}, {a: 1}) // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]}) // You can also use Chai (http://chaijs.com/) by requiring it yourself // var expect = require("chai").expect; // var assert = require("chai").assert; // require("chai").should(); describe("Solution", function(){ it("should test for something", function(){ //Test.assertEquals("actual", "expected", "This is just an example of how you can write your own TDD tests"); Test.assertEquals(sumOfElements([1,2,3]), 6, "Wrong"); Test.assertEquals(sumOfElements([1]), 1, "Wrong"); Test.assertEquals(sumOfElements(), 0, "Wrong"); }); });
- // TODO: Replace examples and use TDD development by writing your own tests
- // These are some CW specific test methods available:
- // Test.expect(boolean, [optional] message)
- // Test.assertEquals(actual, expected, [optional] message)
- // Test.assertSimilar(actual, expected, [optional] message)
- // Test.assertNotEquals(actual, expected, [optional] message)
- // NodeJS assert is also automatically required for you.
- // assert(true)
- // assert.strictEqual({a: 1}, {a: 1})
- // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
- // You can also use Chai (http://chaijs.com/) by requiring it yourself
- // var expect = require("chai").expect;
- // var assert = require("chai").assert;
- // require("chai").should();
- describe("Solution", function(){
- it("should test for something", function(){
- //Test.assertEquals("actual", "expected", "This is just an example of how you can write your own TDD tests");
- Test.assertEquals(sumOfElements([1,2,3]), 6, "Wrong");
Test.assertEquals(sumOfElements(), 15, "Wrong");- Test.assertEquals(sumOfElements([1]), 1, "Wrong");
- Test.assertEquals(sumOfElements(), 0, "Wrong");
- });
- });