-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercisesOscar.ts
76 lines (61 loc) · 2.53 KB
/
ExercisesOscar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* TypeScript Exercises Oscar Arias 09/30/2024 Use: https://www.typescriptlang.org/play/ */
const months_of_the_year: { name: string; days: number }[] = [
{ name: "January", days: 31 },
{ name: "February", days: 28 }, // 29 in a leap year
{ name: "March", days: 31 },
{ name: "April", days: 30 },
{ name: "May", days: 31 },
{ name: "June", days: 30 },
{ name: "July", days: 31 },
{ name: "August", days: 31 },
{ name: "September", days: 30 },
{ name: "October", days: 31 },
{ name: "November", days: 30 },
{ name: "December", days: 31 },
];
/* 1. Map Exercise: Extract the numeric portion of the months_of_the_year array and write a function that returns a new array with each number squared.
Objective: Assess understanding of the map function. */
const square_numbers = [];
console.log(square_numbers);
/* [LOG]: [961, 784, 961, 900, 961, 900, 961, 961, 900, 961, 900, 961] */
/* 2. Filter Exercise: Extract the string portion of the months_of_the_year array, write a function that returns a new array containing only the strings that have more than 6 characters.
Objective: Test knowledge of the filter function.
*/
const long_months = [];
console.log(long_months);
/* [LOG]: ["January", "February", "September", "October", "November", "December"] */
/* ---------------- SOLUTIONS --------------- */
const months_of_the_year: { name: string, days: number }[] = [
{ name: "January", days: 31 },
{ name: "February", days: 28 }, // 29 in a leap year
{ name: "March", days: 31 },
{ name: "April", days: 30 },
{ name: "May", days: 31 },
{ name: "June", days: 30 },
{ name: "July", days: 31 },
{ name: "August", days: 31 },
{ name: "September", days: 30 },
{ name: "October", days: 31 },
{ name: "November", days: 30 },
{ name: "December", days: 31 }
];
/* Task: 1 */
if (isCurrentYearLeapYear()){
months_of_the_year[1].days = 29;
}
const mappedNumbers = months_of_the_year.map(month => month.days ** 2);
/* Task: 2 */
if (isCurrentYearLeapYear()){
months_of_the_year[1].days = 29;
}
const filterdMonths = months_of_the_year
.filter(month => month.name.length > 6)
.map(month => month.name);
console.log(mappedNumbers);
// [961, 784, 961, 900, 961, 900, 961, 961, 900, 961, 900, 961]
console.log(filterdMonths);
// ["January", "February", "September", "October", "November", "December"]
function isCurrentYearLeapYear(): boolean {
const currentYear = new Date().getFullYear();
return (currentYear % 4 === 0 && currentYear % 100 !== 0) || (currentYear % 400 === 0);
}