Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Loading
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions 21 .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check labs

on: pull_request

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Node.js
uses: actions/setup-node@v1
with:
node-version: 14
- uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci
- run: npm t
31 changes: 31 additions & 0 deletions 31 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 8 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 19 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 8 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 15 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 12 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 11 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.