This commit is contained in:
Ilya Kantor 2016-07-31 00:28:27 +03:00
parent 9064e35f3f
commit 4c531b5ae7
371 changed files with 338 additions and 316 deletions

View file

@ -0,0 +1,7 @@
function multiplyNumeric(obj) {
for (let key in obj) {
if (typeof obj[key] == 'number') {
obj[key] *= 2;
}
}
}

View file

@ -0,0 +1,17 @@
let menu = {
width: 200,
height: 300,
title: "My menu"
};
function multiplyNumeric(obj) {
/* your code */
}
multiplyNumeric(menu);
alert( "menu width=" + menu.width + " height=" + menu.height + " title=" + menu.title );

View file

@ -0,0 +1,18 @@
describe("multiplyNumeric", function() {
it("multiplies all numeric properties by 2", function() {
var menu = {
width: 200,
height: 300,
title: "My menu"
};
let result = multiplyNumeric(menu);
assert.equal(menu.width, 400);
assert.equal(menu.height, 600);
assert.equal(menu.title, "My menu");
});
it("returns nothing", function() {
assert.isUndefined( multiplyNumeric({}) );
});
});

View file

@ -0,0 +1,33 @@
importance: 3
---
# Multiply numeric properties by 2
Create a function `multiplyNumeric(obj)` that multiplies all numeric properties of `obj` by `2`.
For instance:
```js
// before the call
let menu = {
width: 200,
height: 300,
title: "My menu"
};
multiplyNumeric(menu);
// after the call
menu = {
width: 400,
height: 600,
title: "My menu"
};
```
Please note that `multiplyNumeric` does not need to return anything. It should modify the object in-place.
P.S. Use `typeof` to check for a number here.