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

1. Реалізуйте функцію `showAllElementOfArray(arr)`, яка приймає масив довільного вигляду та виводить елементи даного масива та масивів, які є елементами.
```js
const arr = [1, [2, 3, [4, [5]]], 6, [33]];

showAllElementOfArray(arr);
```

Результат:
```
1
2
3
4
5
6
```

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

```js
console.log(powLoop(2, 1)); // 2
console.log(powLoop(2, 3)); // 8
console.log(powLoop(2, 5)); // 32
```
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 arr = [1, [2, 3, [4, [5]]], 6, [33]];

const showAllElementOfArray = (arr) => {
for (const elem of arr) {
if (Array.isArray(elem)) showAllElementOfArray(elem);
else console.log(elem);
}
};

showAllElementOfArray(arr);
9 changes: 9 additions & 0 deletions Solutions/2-powloop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

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