-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathлр5.js
More file actions
52 lines (39 loc) · 1.42 KB
/
Copy pathлр5.js
File metadata and controls
52 lines (39 loc) · 1.42 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
// Завдання 1
let styles = ["Jazz", "Blues"]; // [Jazz, Blues]
styles.push("Rock-n-Roll"); // [Jazz, Blues, Rock-n-Roll]
styles[Math.floor(styles.length / 2)] = "Classics"; // [Jazz, Classics, Rock-n-Roll]
alert(styles.shift()); // Jazz, масив: [Classics, Rock-n-Roll]
styles.unshift("Rap", "Reggae"); // [Rap, Reggae, Classics, Rock-n-Roll]
// Завданна 2
function sumInput() {
let numbers = [];
while (true) {
let value = prompt("Введіть число", 0);
// Зупиняємось при відміні, порожньому рядку або не-числі
if (value === "" || value === null || !isFinite(value)) break;
numbers.push(+value);
}
let sum = 0;
for (let number of numbers) {
sum += number;
}
return sum;
}
alert(sumInput());
// Завдання 3
function getMaxSubSum(arr) {
let maxSum = 0;
let currentSum = 0;
for (let item of arr) {
currentSum += item;
if (currentSum < 0) currentSum = 0;
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
// Перевірка:
alert(getMaxSubSum([-1, 2, 3, -9])); // 5
alert(getMaxSubSum([2, -1, 2, 3, -9])); // 6
alert(getMaxSubSum([-1, 2, 3, -9, 11])); // 11
alert(getMaxSubSum([-2, -1, -3, -4])); // 0
alert(getMaxSubSum([100, -9, 2, -3, 5])); // 100