Skip to content

Commit d20b583

Browse files
committed
add exercise2
1 parent 232be72 commit d20b583

File tree

30 files changed

+740
-0
lines changed

30 files changed

+740
-0
lines changed

exercise2/problem1/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem 1
2+
3+
Create a function that takes two numbers as arguments (`num`, `length`) and returns an array of multiples of `num` until
4+
the array length reaches `length`.
5+
6+
```js
7+
console.log(arrayOfMultiples(7, 5)) // [7, 14, 21, 28, 35]
8+
9+
console.log(arrayOfMultiples(12, 10)) // [12, 24, 36, 48, 60, 72, 84, 96, 108, 120]
10+
11+
console.log(arrayOfMultiples(17, 6)) // [17, 34, 51, 68, 85, 102]
12+
```

exercise2/problem1/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function arrayOfMultiples() {
2+
// Your code
3+
}
4+
5+
export default arrayOfMultiples;

exercise2/problem1/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, test } from "vitest";
2+
import arrayOfMultiples from "./index";
3+
4+
describe("exercise2 - problem1", () => {
5+
test.each([
6+
{ num: 7, len: 5, expected: [7, 14, 21, 28, 35] },
7+
{ num: 12, len: 10, expected: [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] },
8+
{ num: 17, len: 7, expected: [17, 34, 51, 68, 85, 102, 119] },
9+
{
10+
num: 630,
11+
len: 14,
12+
expected: [
13+
630, 1260, 1890, 2520, 3150, 3780, 4410, 5040, 5670, 6300, 6930, 7560,
14+
8190, 8820,
15+
],
16+
},
17+
{ num: 140, len: 3, expected: [140, 280, 420] },
18+
{ num: 7, len: 8, expected: [7, 14, 21, 28, 35, 42, 49, 56] },
19+
{
20+
num: 11,
21+
len: 21,
22+
expected: [
23+
11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132, 143, 154, 165, 176,
24+
187, 198, 209, 220, 231,
25+
],
26+
},
27+
])(".arrayOfMultiples($num, $len)", ({ num, len, expected }) => {
28+
expect(arrayOfMultiples(num, len)).toEqual(expected);
29+
});
30+
});

exercise2/problem10/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Problem 10
2+
3+
Create a function that takes an array of objects of structure:
4+
5+
```ts
6+
type InputItem = {
7+
name: string;
8+
notes: number[];
9+
};
10+
11+
type Input = InputItem[];
12+
```
13+
14+
and returns:
15+
16+
```ts
17+
type ReturnItem = {
18+
name: string;
19+
topNote: number;
20+
};
21+
22+
type Return = ReturnItem[];
23+
```
24+
25+
If student has no notes (an empty array) then let's assume `topNote: 0`.
26+
27+
```js
28+
console.log(
29+
getStudentsWithNamesAndTopNotes([
30+
{name: "John", notes: [3, 5, 4]},
31+
{name: "Max", notes: [1, 4, 6]},
32+
{name: "Zygmund", notes: [1, 2, 3]},
33+
])
34+
)
35+
// [
36+
// ({ name: "John", topNote: 5 },
37+
// { name: "Max", topNote: 6 },
38+
// { name: "Zygmund", topNote: 3 })
39+
// ];
40+
```

exercise2/problem10/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function getStudentsWithNamesAndTopNotes() {
2+
// Your code
3+
}
4+
5+
export default getStudentsWithNamesAndTopNotes;

exercise2/problem10/index.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, test } from "vitest";
2+
import getStudentsWithNamesAndTopNotes from "./index";
3+
4+
describe("exercise2 - problem10", () => {
5+
test.each([
6+
{
7+
students: [
8+
{ name: "John", notes: [3, 5, 4] },
9+
{ name: "Max", notes: [1, 4, 6] },
10+
{ name: "Zygmund", notes: [1, 2, 3] },
11+
],
12+
expected: [
13+
{ name: "John", topNote: 5 },
14+
{ name: "Max", topNote: 6 },
15+
{ name: "Zygmund", topNote: 3 },
16+
],
17+
},
18+
{
19+
students: [
20+
{ name: "John", notes: [5, 4, 3] },
21+
{ name: "Max", notes: [3, 3, 3] },
22+
{ name: "Zygmund", notes: [1, 2, 3] },
23+
],
24+
expected: [
25+
{ name: "John", topNote: 5 },
26+
{ name: "Max", topNote: 3 },
27+
{ name: "Zygmund", topNote: 3 },
28+
],
29+
},
30+
{
31+
students: [
32+
{ name: "John", notes: [] },
33+
{ name: "Ewa", notes: [] },
34+
{ name: "Zygmund", notes: [1, 2, 3] },
35+
],
36+
expected: [
37+
{ name: "John", topNote: 0 },
38+
{ name: "Ewa", topNote: 0 },
39+
{ name: "Zygmund", topNote: 3 },
40+
],
41+
},
42+
])(
43+
".getStudentsWithNamesAndTopNotes($students)",
44+
({ students, expected }) => {
45+
expect(getStudentsWithNamesAndTopNotes(students)).toEqual(expected);
46+
}
47+
);
48+
});

exercise2/problem2/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Problem 2
2+
3+
Create a function that, given an array where elements of the array are either an _integer or an array containing a
4+
single integer_, sorts the array according to the "content of the elements".
5+
6+
```js
7+
console.log(sortIt([4, 1, 3])) // [1, 3, 4]
8+
9+
console.log(sortIt([[4], [1], [3]])) // [[1], [3], [4]]
10+
11+
console.log(sortIt([4, [1], 3])) // [[1], 3, 4]
12+
13+
console.log(sortIt([[4], 1, [3]])) // [1, [3], [4]]
14+
15+
console.log(sortIt([[3], 4, [2], [5], 1, 6])) // [1, [2], [3], 4, [5], 6]
16+
```

exercise2/problem2/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function sortIt() {
2+
// Your code
3+
}
4+
5+
export default sortIt;

exercise2/problem2/index.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, test } from "vitest";
2+
import sortIt from "./index";
3+
4+
describe("exercise2 - problem2", () => {
5+
test.each([
6+
{ arr: [4, 1, 3], expected: [1, 3, 4] },
7+
{ arr: [[4], [1], [3]], expected: [[1], [3], [4]] },
8+
{ arr: [4, [1], 3], expected: [[1], 3, 4] },
9+
{ arr: [[4], 1, [3]], expected: [1, [3], [4]] },
10+
{ arr: [[3], 4, [2], [5], 1, 6], expected: [1, [2], [3], 4, [5], 6] },
11+
{ arr: [[3], 7, [9], [5], 1, 6], expected: [1, [3], [5], 6, 7, [9]] },
12+
{
13+
arr: [[3], 7, [9], [5], 1, 6, [0]],
14+
expected: [[0], 1, [3], [5], 6, 7, [9]],
15+
},
16+
])(".sortIt($arr)", ({ arr, expected }) => {
17+
expect(sortIt(arr)).toEqual(expected);
18+
});
19+
});

exercise2/problem3/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem 3
2+
3+
Arrays can be mixed with various types. Your task for this challenge is to sum all the number elements in the given
4+
array. Create a function that takes an array and returns the sum of all numbers in the array.
5+
6+
```js
7+
console.log(numbersSum([1, 2, "13", "4", "645"])) // 3
8+
9+
console.log(numbersSum([true, false, "123", "75"])) // 0
10+
11+
console.log(numbersSum([1, 2, 3, 4, 5, true])) // 15
12+
```

0 commit comments

Comments
 (0)