countProperties

Easy · Object Manipulation · 3 test cases

Given an object, return the total number of properties it has.

Examples

countProperties({a: 1, b: 2, c: 3}) → 3
countProperties({}) → 0

Starter code

function countProperties(obj) {

}

Complexity of the optimal solution

Time O(n), space O(n). `Object.keys(obj)` creates an array containing all 'n' property names of the object. This operation takes O(n) time and O(n) space. Getting the length of the array is then a constant time operation.

Solve countProperties in the browser