Skip to content

Commit d5382ec

Browse files
committed
Add exercises
1 parent a2f4fea commit d5382ec

File tree

10 files changed

+130
-0
lines changed

10 files changed

+130
-0
lines changed

Exercises/1-for.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const sum = (...args) => {
4+
// Use for loop and accumulator variable
5+
// to calculate sum of all given arguments
6+
// For example sum(1, 2, 3) should return 6
7+
};
8+
9+
module.exports = { sum };

Exercises/1-for.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
({
2+
name: 'sum',
3+
length: [70, 130],
4+
cases: [
5+
[1, 2, 3, 6],
6+
[0, 0],
7+
[0],
8+
[1, -1, 1, 1],
9+
[10, -1, -1, -1, 7],
10+
],
11+
test: sum => {
12+
const src = sum.toString();
13+
if (!src.includes('for (')) throw new Error('Use for loop');
14+
if (!src.includes('for (let')) throw new Error('Use let for accumulator');
15+
if (!src.includes('return')) throw new Error('Use return');
16+
}
17+
})

Exercises/2-for-of.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const sum = (...args) => {
4+
// Use for..of loop and accumulator variable
5+
// to calculate sum of all given arguments
6+
// For example sum(1, 2, 3) should return 6
7+
};
8+
9+
module.exports = { sum };

Exercises/2-for-of.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
({
2+
name: 'sum',
3+
length: [70, 130],
4+
cases: [
5+
[1, 2, 3, 6],
6+
[0, 0],
7+
[0],
8+
[1, -1, 1, 1],
9+
[10, -1, -1, -1, 7],
10+
],
11+
test: sum => {
12+
const src = sum.toString();
13+
if (!src.includes('for (')) throw new Error('Use for..of loop');
14+
if (!src.includes(' of ')) throw new Error('Use for..of in loop');
15+
if (!src.includes('for (const')) throw new Error('Use const in loop');
16+
if (!src.includes('let')) throw new Error('Use let to define accumulator');
17+
if (!src.includes('return')) throw new Error('Use return');
18+
}
19+
})

Exercises/3-while.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const sum = (...args) => {
4+
// Use while loop and accumulator variable
5+
// to calculate sum of all given arguments
6+
// For example sum(1, 2, 3) should return 6
7+
};
8+
9+
module.exports = { sum };

Exercises/3-while.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
({
2+
name: 'sum',
3+
length: [100, 130],
4+
cases: [
5+
[1, 2, 3, 6],
6+
[0, 0],
7+
[0],
8+
[1, -1, 1, 1],
9+
[10, -1, -1, -1, 7],
10+
],
11+
test: sum => {
12+
const src = sum.toString();
13+
if (!src.includes('while (')) throw new Error('Use while loop');
14+
if (!src.includes('let')) throw new Error('Use let to define accumulator');
15+
if (!src.includes('return')) throw new Error('Use return');
16+
}
17+
})

Exercises/4-do-while.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const sum = (...args) => {
4+
// Use do..while loop and accumulator variable
5+
// to calculate sum of all given arguments
6+
// For example sum(1, 2, 3) should return 6
7+
};
8+
9+
module.exports = { sum };

Exercises/4-do-while.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
({
2+
name: 'sum',
3+
length: [130, 160],
4+
cases: [
5+
[1, 2, 3, 6],
6+
[0, 0],
7+
[0],
8+
[1, -1, 1, 1],
9+
[10, -1, -1, -1, 7],
10+
],
11+
test: sum => {
12+
const src = sum.toString();
13+
if (!src.includes('while (')) throw new Error('Use while loop');
14+
if (!src.includes('let')) throw new Error('Use let to define accumulator');
15+
if (!src.includes('return')) throw new Error('Use return');
16+
}
17+
})

Exercises/5-reduce.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const sum = (...args) => 0;
4+
// Use Array.prototype.reduce method
5+
// to calculate sum of all given arguments
6+
// For example sum(1, 2, 3) should return 6
7+
8+
module.exports = { sum };

Exercises/5-reduce.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
({
2+
name: 'sum',
3+
length: [40, 60],
4+
cases: [
5+
[1, 2, 3, 6],
6+
[0, 0],
7+
[0],
8+
[1, -1, 1, 1],
9+
[10, -1, -1, -1, 7],
10+
],
11+
test: sum => {
12+
const src = sum.toString();
13+
if (!src.includes('.reduce(')) throw new Error('Use reduce method');
14+
if (src.includes('return')) throw new Error('Do not use return');
15+
}
16+
})

0 commit comments

Comments
 (0)