Skip to content

Commit 408ba7d

Browse files
committed
make sure a solution always shows up, use "demo" if exists
1 parent 360be9e commit 408ba7d

File tree

25 files changed

+59
-95
lines changed

25 files changed

+59
-95
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
11
Just loop over the object and `return false` immediately if there's at least one property.
2-
3-
```js
4-
function isEmpty(obj) {
5-
for (let key in obj) {
6-
return false;
7-
}
8-
return true;
9-
}
10-
```
Original file line numberDiff line numberDiff line change
@@ -1,19 +0,0 @@
1-
``` js run
2-
3-
// before the call
4-
let menu = {
5-
width: 200,
6-
height: 300,
7-
title: "My menu"
8-
};
9-
10-
function multiplyNumeric(obj) {
11-
for (let key in obj) {
12-
if (typeof obj[key] == 'number') {
13-
obj[key] *= 2;
14-
}
15-
}
16-
}
17-
18-
alert(menu);
19-
```

1-js/04-object-basics/04-object-methods/7-calculator/solution.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

2-
3-
```js run demo
2+
```js run demo solution
43
let calculator = {
54
sum() {
65
return this.a + this.b;
@@ -20,4 +19,3 @@ calculator.read();
2019
alert( calculator.sum() );
2120
alert( calculator.mul() );
2221
```
23-

1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The solution is to return the object itself from every call.
22

3-
```js run
3+
```js run demo
44
let ladder = {
55
step: 0,
66
up() {
@@ -28,7 +28,7 @@ ladder.up().up().down().up().down().showStep(); // 1
2828

2929
We also can write a single call per line. For long chains it's more readable:
3030

31-
```js
31+
```js
3232
ladder
3333
.up()
3434
.up()
@@ -37,4 +37,3 @@ ladder
3737
.down()
3838
.showStep(); // 1
3939
```
40-

1-js/04-object-basics/06-constructor-new/2-calculator-constructor/solution.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
```js run demo
42
function Calculator() {
53

1-js/05-data-types/03-string/3-truncate/solution.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ The maximal length must be `maxlength`, so we need to cut it a little shorter, t
22

33
Note that there is actually a single unicode character for an ellipsis. That's not three dots.
44

5-
```js run
5+
```js run demo
66
function truncate(str, maxlength) {
7-
return (str.length > maxlength) ?
7+
return (str.length > maxlength) ?
88
str.slice(0, maxlength - 1) + '' : str;
99
}
1010
```
11-
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
```js run
2-
function extractCurrencyValue(str) {
3-
return +str.slice(1);
4-
}
5-
```

1-js/05-data-types/04-array/10-maximal-subarray/solution.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# The slow solution
1+
# Slow solution
22

33
We can calculate all possible subsums.
44

@@ -67,7 +67,7 @@ Let's walk the array and keep the current partial sum of elements in the variabl
6767

6868
If the description is too vague, please see the code, it's short enough:
6969

70-
```js run
70+
```js run demo
7171
function getMaxSubSum(arr) {
7272
let maxSum = 0;
7373
let partialSum = 0;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function camelize(str) {
22
return str
3-
.split('-') // my-long-word -> ['my', 'long', 'word']
4-
.map(
3+
.split('-') // splits 'my-long-word' into array ['my', 'long', 'word']
4+
.map(
5+
// capitalizes first letters of all array items except the first one
6+
// converts ['my', 'long', 'word'] into ['my', 'Long', 'Word']
57
(word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
6-
) // ['my', 'long', 'word'] -> ['my', 'Long', 'Word']
7-
.join(''); // ['my', 'Long', 'Word'] -> myLongWord
8+
)
9+
.join(''); // joins ['my', 'Long', 'Word'] into 'myLongWord'
810
}

1-js/05-data-types/05-array-methods/11-array-unique/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Let's walk the array items:
22
- For each item we'll check if the resulting array already has that item.
33
- If it is so, then ignore, otherwise add to results.
44

5-
```js run
5+
```js run demo
66
function unique(arr) {
77
let result = [];
88

1-js/05-data-types/05-array-methods/2-filter-range/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
``` js run
1+
```js run demo
22
function filterRange(arr, a, b) {
33
// added brackets around the expression for better readability
44
return arr.filter(item => (a <= item && item <= b));

1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
function filterRangeInPlace(arr, a, b) {
43

54
for (let i = 0; i < arr.length; i++) {
@@ -12,4 +11,4 @@ function filterRangeInPlace(arr, a, b) {
1211
}
1312
}
1413

15-
}
14+
}

1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
``` js run
1+
```js run demo
22
function filterRangeInPlace(arr, a, b) {
33

44
for (let i = 0; i < arr.length; i++) {
@@ -18,5 +18,4 @@ let arr = [5, 3, 8, 1];
1818
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
1919

2020
alert( arr ); // [3, 1]
21-
2221
```

1-js/05-data-types/07-map-set-weakmap-weakset/02-filter-anagrams/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Here we could also use a plain object instead of the `Map`, because keys are str
5959

6060
That's how the solution can look:
6161

62-
```js run
62+
```js run demo
6363
function aclean(arr) {
6464
let obj = {};
6565

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
```js run
1+
```js run demo
22
function sumSalaries(salaries) {
33

44
let sum = 0;
@@ -8,7 +8,22 @@ function sumSalaries(salaries) {
88

99
return sum; // 650
1010
}
11+
12+
let salaries = {
13+
"John": 100,
14+
"Pete": 300,
15+
"Mary": 250
16+
};
17+
18+
alert( sumSalaries(salaries) ); // 650
1119
```
1220
Or, optionally, we could also get the sum using `Object.values` and `reduce`:
1321

14-
`Object.values(salaries).reduce((a, b) => a + b) // 650`
22+
```js
23+
// reduce loops over array of salaries,
24+
// adding them up
25+
// and returns the result
26+
function sumSalaries(salaries) {
27+
return Object.values(salaries).reduce((a, b) => a + b, 0) // 650
28+
}
29+
```

1-js/05-data-types/10-date/2-get-week-day/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ The method `date.getDay()` returns the number of the weekday, starting from sund
22

33
Let's make an array of weekdays, so that we can get the proper day name by its number:
44

5-
```js run
5+
```js run demo
66
function getWeekDay(date) {
77
let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
88

Original file line numberDiff line numberDiff line change
@@ -1,14 +0,0 @@
1-
```js run
2-
function getLocalDay(date) {
3-
4-
let day = date.getDay();
5-
6-
if (day == 0) { // 0 becomes 7
7-
day = 7;
8-
}
9-
10-
return day;
11-
}
12-
13-
alert( getLocalDay(new Date(2012, 0, 3)) ); // 2
14-
```

1-js/05-data-types/10-date/4-get-date-ago/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function getDateAgo(date, days) {
1111

1212
To implement it let's clone the date, like this:
1313

14-
```js run
14+
```js run demo
1515
function getDateAgo(date, days) {
1616
let dateCopy = new Date(date);
1717

1-js/05-data-types/10-date/5-last-day-of-month/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Let's create a date using the next month, but pass zero as the day:
2-
```js run
2+
```js run demo
33
function getLastDayOfMonth(year, month) {
44
let date = new Date(year, month + 1, 0);
55
return date.getDate();

1-js/05-data-types/10-date/8-format-date-relative/solution.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
To get the time from `date` till now -- let's substract the dates.
22

3-
```js run
3+
```js run demo
44
function formatDate(date) {
55
let diff = new Date() - date; // the difference in milliseconds
66

@@ -57,12 +57,12 @@ function formatDate(date) {
5757
let diffSec = Math.round(diffMs / 1000);
5858
let diffMin = diffSec / 60;
5959
let diffHour = diffMin / 60;
60-
60+
6161
// formatting
6262
year = year.toString().slice(-2);
6363
month = month < 10 ? '0' + month : month;
6464
dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;
65-
65+
6666
if (diffSec < 1) {
6767
return 'right now';
6868
} else if (diffMin < 1) {

1-js/06-advanced-functions/03-closure/6-filter-through-function/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ alert( arr.filter(inBetween(3, 6)) ); // 3,4,5,6
1414

1515
# Filter inArray
1616

17-
```js run
17+
```js run demo
1818
function inArray(arr) {
1919
return function(x) {
2020
return arr.includes(x);

1-js/06-advanced-functions/03-closure/8-make-army/solution.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function makeArmy() {
5555

5656
As a result, all `shooter` functions get from the outer lexical envrironment the same, last value `i=10`.
5757

58-
The fix can be very simple:
58+
We can fix it by moving the variable definition into the loop:
5959

60-
```js run
60+
```js run demo
6161
function makeArmy() {
6262

6363
let shooters = [];
@@ -80,17 +80,16 @@ army[0](); // 0
8080
army[5](); // 5
8181
```
8282

83-
Now it works correctly, because every time the code block in `for (..) {...}` is executed, a new Lexical Environment is created for it, with the corresponding value of `i`.
83+
Now it works correctly, because every time the code block in `for (let i=0...) {...}` is executed, a new Lexical Environment is created for it, with the corresponding variable `i`.
8484

85-
So, the value of `i` now lives a little bit closer. Not in `makeArmy()` Lexical Environment, but in the Lexical Environment that corresponds the current loop iteration. A `shooter` gets the value exactly from the one where it was created.
85+
So, the value of `i` now lives a little bit closer. Not in `makeArmy()` Lexical Environment, but in the Lexical Environment that corresponds the current loop iteration. That's why now it works.
8686

8787
![](lexenv-makearmy.png)
8888

8989
Here we rewrote `while` into `for`.
9090

9191
Another trick could be possible, let's see it for better understanding of the subject:
9292

93-
9493
```js run
9594
function makeArmy() {
9695
let shooters = [];

1-js/06-advanced-functions/09-call-apply-decorators/02-delay/solution.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
The solution:
22

3-
```js
3+
```js run demo
44
function delay(f, ms) {
55

66
return function() {
77
setTimeout(() => f.apply(this, arguments), ms);
88
};
99

1010
}
11+
12+
let f1000 = delay(alert, 1000);
13+
14+
f1000("test"); // shows "test" after 1000ms
1115
```
1216

1317
Please note how an arrow function is used here. As we know, arrow functions do not have own `this` and `arguments`, so `f.apply(this, arguments)` takes `this` and `arguments` from the wrapper.
1418

15-
If we pass a regular function, `setTimeout` would call it without arguments and `this=window` (in-browser), so we'd need to write a bit more code to pass them from the wrapper:
19+
If we pass a regular function, `setTimeout` would call it without arguments and `this=window` (assuming we're in the browser).
20+
21+
We still can pass the right `this` by using an intermediate variable, but that's a little bit more cumbersome:
1622

1723
```js
1824
function delay(f, ms) {
1925

20-
// added variables to pass this and arguments from the wrapper inside setTimeout
2126
return function(...args) {
22-
let savedThis = this;
27+
let savedThis = this; // store this into an intermediate variable
2328
setTimeout(function() {
24-
f.apply(savedThis, args);
29+
f.apply(savedThis, args); // use it here
2530
}, ms);
2631
};
2732

1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/solution.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
2-
3-
```js run no-beautify
1+
```js demo
42
function debounce(f, ms) {
53

64
let isCooldown = false;
@@ -18,7 +16,7 @@ function debounce(f, ms) {
1816
}
1917
```
2018

21-
The call to `debounce` returns a wrapper. There may be two states:
19+
A call to `debounce` returns a wrapper. There may be two states:
2220

2321
- `isCooldown = false` -- ready to run.
2422
- `isCooldown = true` -- waiting for the timeout.

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
```js
1+
```js demo
22
function throttle(func, ms) {
33

44
let isThrottled = false,

0 commit comments

Comments
 (0)