Overview
Implement size(collection) — returns length for arrays/strings, key count for objects.
Examples
size([1,2,3]); // 3
size({a:1,b:2}); // 2
size('hello'); // 5Solution
Reveal solution
function size(collection) {
if (collection == null) return 0;
if (Array.isArray(collection) || typeof collection === 'string') return collection.length;
return Object.keys(collection).length;
}collection-size.js
Size
easycodingJavaScriptFunctions
Overview
Implement size(collection) — returns length for arrays/strings, key count for objects.
Examples
size([1,2,3]); // 3
size({a:1,b:2}); // 2
size('hello'); // 5Solution
Reveal solution
function size(collection) {
if (collection == null) return 0;
if (Array.isArray(collection) || typeof collection === 'string') return collection.length;
return Object.keys(collection).length;
}NameTopicDifficulty
103 of 103 problems