works
This commit is contained in:
parent
d813c3dd19
commit
b043ea701d
31 changed files with 600 additions and 408 deletions
|
@ -0,0 +1,4 @@
|
|||
function count(obj) {
|
||||
return Object.keys(obj).length;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
describe("count", function() {
|
||||
it("counts the number of properties", function() {
|
||||
assert.equal( count({a: 1, b: 2}), 2 );
|
||||
});
|
||||
|
||||
it("returns 0 for an empty object", function() {
|
||||
assert.equal( count({}), 0 );
|
||||
});
|
||||
|
||||
it("ignores symbolic properties", function() {
|
||||
assert.equal( count({ [Symbol('id')]: 1 }), 0 );
|
||||
});
|
||||
});
|
21
1-js/4-data-structures/4-object/5-count-properties/task.md
Normal file
21
1-js/4-data-structures/4-object/5-count-properties/task.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
importance: 5
|
||||
|
||||
---
|
||||
|
||||
# Count properties
|
||||
|
||||
Write a function `count(obj)` that returns the number of properties in the object:
|
||||
|
||||
```js
|
||||
let user = {
|
||||
name: 'John',
|
||||
age: 30
|
||||
};
|
||||
|
||||
alert( count(user) ); // 2
|
||||
```
|
||||
|
||||
Try to make the code as short as possible.
|
||||
|
||||
P.S. Ignore symbolic properties, count only "regular" ones.
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue