Skip to content

Add exercises #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Recursion.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Exercise.ua.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Рекурсія

1. Реалізуйте функцію `deepSum(nested)`, яка приймає масив чисел та вкладених масивів довільної глибини та повертає суму всіх чисел вкладених масивів.
```js
const numbers = [1, [2, 3, [4, [5]]], 6, [33]];
const sum = deepSum(numbers);
console.log(sum);
```

Результат:
```
54
```

### Використання циклів

2. Реалізуйте функцію `pow(base, power)`, яка працює ідентично наступній, але з використанням циклу:
```js
const pow = (base, power) => {
if (power === 0) return 1;
return pow(base, power - 1) * base;
}
```

Приклади:

```js
console.log(pow(2, 1)); // 2
console.log(pow(2, 3)); // 8
console.log(pow(2, 5)); // 32
```
8 changes: 8 additions & 0 deletions Exercises/1-showelem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const deepSum = (array) => {
// use recursion to sum all element of array
// deepSum([1, [2, 3, [4, [5]]], 6, [33]]) = 54
};

module.exports = { deepSum };
19 changes: 19 additions & 0 deletions Exercises/1-showelem.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
({
name: 'deepSum',
length: [150, 230],
cases: [
[[1, 2, 3], 6],
[[0], 0],
[[1, -1, 1], 1],
[[10, -1, -1, -1], 7],
[[1, [2, 3, [4, [5]]], 6, [33]], 54],
],
test(deepSum) {
const src = deepSum.toString();
const numbers = [1, 'string', [12, 3]];
const sum = deepSum(numbers);
if (isNaN(sum)) throw new Error('Do not add string');
if (!src.includes('deepSum(')) throw new Error('Use recursion');
if (!src.includes('return')) throw new Error('Use return');
}
})
8 changes: 8 additions & 0 deletions Exercises/2-powloop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const pow = (base, power) => {
// use loop to raise a number to a power
// do not use recursion
};

module.exports = { pow };
15 changes: 15 additions & 0 deletions Exercises/2-powloop.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
({
name: 'pow',
length: [110, 140],
cases: [
[2, 1, 2],
[2, 2, 4],
[2, 3, 8],
[2, 10, 1024]
],
test(pow) {
const src = pow.toString();
if (src.includes('pow(')) throw new Error('Do not use recursion');
if (!src.includes('return')) throw new Error('Use return');
}
})
12 changes: 12 additions & 0 deletions Solutions/1-showelem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const deepSum = (array) => {
let sum = 0;
for (const element of array) {
if (typeof element === 'number') sum += element;
if (Array.isArray(element)) sum += deepSum(element);
}
return sum;
};

module.exports = { deepSum };
11 changes: 11 additions & 0 deletions Solutions/2-powloop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const pow = (base, power) => {
let result = 1;
for (let i = 0; i < power; i++) {
result *= base;
}
return result;
};

module.exports = { pow };
Loading