@cmarmar/

Key Adder

ES6

No description

fork
loading
main.js
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
// Create a Key Adder that loops over an Object's keys, finds(filters) all number values, and returns
// the sum of all the numbers.

// Example:

// ```
// // INPUT: {a0:0, a1:1, a2:2, a3:3, a4:4}
// keyAdder.call({a0:0, a1:1, a2:2, a3:3, a4:4})
// // OUTPUT: 10;

// ```

function keyAdder(){
  let sumOfNums = 0
  for (const key in this){
    if (this.hasOwnProperty([key]) && typeof this[key] === 'number'){
      sumOfNums+= this[key]
    }
  }
  return sumOfNums
}


const myObj = {a0: keyAdder, a1:1, a2: 2}
const total = keyAdder.call({a1:4,a2:5,a3:6})
const thirtySix = keyAdder.call({a1:4, a2:22, a3:[1,2,3], a4:"Fullstack", a5:10})
console.log(thirtySix)
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie