Created
December 18, 2019 15:47
-
-
Save jasp402/cf953fdced3b18a98691dca0c31e66b8 to your computer and use it in GitHub Desktop.
Sum all numeric elements from array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
selfSum = a => (Array.isArray(a) && eval(a.filter(n => typeof n === 'number').join('+'))) | 0; | |
describe('TEST # 1 - selfSum()', function () { | |
it('should, sum all numeric elements', function () { | |
let arr = [1,2,3,4,5,6,'A']; | |
assert.equal(selfSum(arr),21); | |
}); | |
it('should, omit characters not numeric', function () { | |
let arr = ['A','b','c']; | |
assert.equal(selfSum(arr),0); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment