forked from vsb-vaj/template-lab-2024s-01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-array.js
More file actions
169 lines (141 loc) · 4.38 KB
/
task-array.js
File metadata and controls
169 lines (141 loc) · 4.38 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// EXERCISE:
// You can write all the code natively (for cycles and such) or you can use some advanced functions which JS arrays provides in case you see them fit
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
// There can be multiple solutions to each task, solve it as you see fit.
// You will work with array `numbers` which will be generated by code (code below) in every task.
// Write these functions:
// This block generates array with random length with values between 1-100
let numbers = [];
const length = Math.ceil(Math.random() * 10) + 5;
for (let i = 0; i < length; i = i + 1) {
numbers.push(Math.ceil(Math.random() * 100));
}
// a) Function which will print to console a whole array
const printArray = (numbers) => {
// Your code:
console.log(numbers)
};
//otestovani
console.log("printArray");
printArray(numbers);
// b) Function which will print to console the length of array
const printLength = (numbers) => {
// Your code:
console.log(numbers.length);
};
console.log("printLength");
printLength(numbers);
// c) Function which will print to console the first element of array
const printFirstItem = (numbers) => {
// Your code:
console.log(numbers[0]);
};
console.log("printFirstItem");
printFirstItem(numbers);
// d) Function which will print to console the last element
const printLastItem = (numbers) => {
// Your code:
console.log(numbers[numbers.length - 1])
};
console.log("printLastItem");
printLastItem(numbers);
// e) Function which will print to console the largest number (You can check Math functions)
const printLargestItem = (numbers) => {
// Your code:
console.log(Math.max(...numbers));
};
console.log("printLargestItem");
printLargestItem(numbers);
// f) Function which will print to console the smallest number (You can check Math functions)
const printSmallestItem = (numbers) => {
// Your code:
console.log(Math.min(...numbers));
};
console.log("printSmallestItem");
printSmallestItem(numbers);
// g) Function which will print to console the sum of all numbers in array (You can check reduce function)
const printSum = (numbers) => {
// Your code:
let sum = 0;
for(let i = 0; i < numbers.length; i++)
{
sum += numbers[i];
}
console.log(sum);
};
console.log("printSum");
printSum(numbers);
// h) Function which will print to console the difference between the largest and the smallest number (You can check Math functions)
const printSALDifference = (numbers) => {
// Your code:
let max_num = Math.max(...numbers);
let min_num = Math.min(...numbers);
let sum = max_num - min_num;
console.log(sum);
};
console.log("printSALDifference");
printSALDifference(numbers);
// i) Function which will print to console the average of all numbers (You can check reduce function)
const printAverage = (numbers) => {
// Your code:
let average = 0.0;
let sum = 0;
for(let i = 0; i < numbers.length; i++)
{
sum += numbers[i];
}
average = sum / numbers.length;
console.log(average);
};
console.log("printAverage");
printAverage(numbers);
// j) Function which will print to console the index of largest number (You can check Math functions)
const printLargestsIndex = (numbers) => {
// Your code:
let max_index = 0;
let current_largest = 0;
for(let i = 0; i < numbers.length; i++)
{
if(numbers[i] > current_largest)
{
max_index = i;
current_largest = numbers[i];
}
}
console.log(max_index);
};
console.log("printLargestIndex");
printLargestsIndex(numbers);
// k) Function which will print to console the even numbers (not the array of even numbers),
// if array doesn't contain any even number, show text "Even number isn't in array"
const printEvenNums = (numbers) => {
// Your code:
let count = 0;
for(let i = 0; i < numbers.length; i++)
{
if(numbers[i] % 2 == 0)
{
count++;
console.log(numbers[i]);
}
}
if(count == 0)
{
console.log("Even number isn't in array");
}
};
console.log("printEvenNums");
printEvenNums(numbers);
// l) Function which will multiple by 2 every number in array and print the array to console
// Example: printNumsMultipliedBy2([1,2,3]) -> [2,4,6]
const printNumsMultipliedBy2 = (numbers) => {
// Your code:
let new_arr = [];
for(let i = 0; i < numbers.length; i++)
{
new_arr.push(numbers[i] * 2);
}
console.log(new_arr);
};
console.log("printNumsMultipliedBy2");
printNumsMultipliedBy2(numbers);