Skip to content

Commit 4f64d7f

Browse files
new tasks
1 parent ff02a3b commit 4f64d7f

10 files changed

+261
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const solve = (arr, delimiter) => {
2+
let result = '';
3+
4+
for (let i = 0; i < arr.length; i++) {
5+
result += i === arr.length - 1 ? arr[i] : (arr[i] + delimiter);
6+
7+
// if (i === arr.length - 1) {
8+
// result += arr[i]
9+
// } else {
10+
// result += (arr[i] + delimiter)
11+
// }
12+
13+
}
14+
15+
return result;
16+
17+
}
18+
19+
console.log(solve(['One',
20+
'Two',
21+
'Three',
22+
'Four',
23+
'Five'],
24+
'-'
25+
))
26+
27+
console.log(solve(['How about no?',
28+
'I',
29+
'will',
30+
'not',
31+
'do',
32+
'it!'],
33+
'_'
34+
))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const solve = (arr, delimiter) => {
2+
return arr.join(delimiter)
3+
4+
5+
}
6+
7+
console.log(solve(['One',
8+
'Two',
9+
'Three',
10+
'Four',
11+
'Five'],
12+
'-'
13+
))
14+
15+
console.log(solve(['How about no?',
16+
'I',
17+
'will',
18+
'not',
19+
'do',
20+
'it!'],
21+
'_'
22+
))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const solve = (arr, step) => {
2+
3+
let result = [];
4+
5+
for (let i = 0; i < arr.length; i++) {
6+
if (i % step === 0) {
7+
result.push(arr[i]);
8+
}
9+
}
10+
11+
return result;
12+
13+
14+
}
15+
16+
console.log(solve(['5',
17+
'20',
18+
'31',
19+
'4',
20+
'20'],
21+
2
22+
));
23+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const solve = (arr, step) => {
2+
3+
return result = arr.filter((el, index) => index % step === 0);
4+
5+
}
6+
7+
console.log(solve(['5',
8+
'20',
9+
'31',
10+
'4',
11+
'20'],
12+
2
13+
));
14+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function addAndRemoveElements(input) {
2+
let result = [];
3+
let number = 1;
4+
5+
for (let i = 0; i < input.length; i++) {
6+
if (input[i] === 'add') {
7+
result.push(number);
8+
} else if (input[i] === 'remove') {
9+
result.pop();
10+
}
11+
number++;
12+
}
13+
14+
return result.length !== 0 ? result.join("\n") : "Empty";
15+
16+
17+
}
18+
19+
console.log(addAndRemoveElements(['add',
20+
'add',
21+
'add',
22+
'add']
23+
));
24+
25+
26+
console.log(addAndRemoveElements(['add',
27+
'add',
28+
'remove',
29+
'add',
30+
'add']
31+
));
32+
33+
console.log(addAndRemoveElements(['remove',
34+
'remove',
35+
'remove']
36+
));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function addAndRemoveElements(input) {
2+
3+
4+
let output = [];
5+
let count = 1;
6+
7+
input.forEach(command => {
8+
if (command === 'add') {
9+
output.push(count);
10+
} else {
11+
output.pop();
12+
}
13+
count++;
14+
})
15+
16+
output.length === 0 ? console.log('Empty') : console.log(output.join('\n'));
17+
18+
}
19+
20+
addAndRemoveElements(['add',
21+
'add',
22+
'add',
23+
'add']
24+
)
25+
26+
27+
addAndRemoveElements(['add',
28+
'add',
29+
'remove',
30+
'add',
31+
'add']
32+
)
33+
34+
addAndRemoveElements(['remove',
35+
'remove',
36+
'remove']
37+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function rotateArray(input, count) {
2+
3+
for (let i = 0; i < count; i++) {
4+
let rightElement = input.pop();
5+
input.unshift(rightElement);
6+
7+
}
8+
return input.join(' ');
9+
10+
}
11+
12+
13+
console.log(rotateArray(['1',
14+
'2',
15+
'3',
16+
'4'],
17+
2
18+
));
19+
20+
console.log(rotateArray(['Banana',
21+
'Orange',
22+
'Coconut',
23+
'Apple'],
24+
15
25+
));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function extractIncreasingSubsequenceFromArray(input) {
2+
3+
let result = [];
4+
let saveLastElement = 0;
5+
for (let i of input) {
6+
7+
if (saveLastElement <= i) {
8+
result.push(i);
9+
saveLastElement = i;
10+
}
11+
}
12+
return result;
13+
14+
15+
}
16+
17+
console.log(extractIncreasingSubsequenceFromArray([1,
18+
3,
19+
8,
20+
4,
21+
10,
22+
12,
23+
3,
24+
2,
25+
24]
26+
));
27+
28+
console.log(extractIncreasingSubsequenceFromArray([1,
29+
2,
30+
3,
31+
4]
32+
));
33+
34+
console.log(extractIncreasingSubsequenceFromArray([20,
35+
3,
36+
2,
37+
15,
38+
6,
39+
1]
40+
));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function extractIncreasingSubsequenceFromArray(input) {
2+
return input.reduce((a, v) => {
3+
if (v >= (a[a.length - 1] || input[0])) {
4+
a.push(v)
5+
}
6+
return a;
7+
}, [])
8+
}
9+
10+
console.log(extractIncreasingSubsequenceFromArray([1,
11+
3,
12+
8,
13+
4,
14+
10,
15+
12,
16+
3,
17+
2,
18+
24]));
19+
20+
console.log(extractIncreasingSubsequenceFromArray([1,
21+
2,
22+
3,
23+
4]));
24+
25+
console.log(extractIncreasingSubsequenceFromArray([20,
26+
3,
27+
2,
28+
15,
29+
6,
30+
1]));

javascript__advanced/04.arraysAndNestedArraysExercise/demo.js

Whitespace-only changes.

0 commit comments

Comments
 (0)