-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson5.js
More file actions
145 lines (107 loc) · 2.89 KB
/
lesson5.js
File metadata and controls
145 lines (107 loc) · 2.89 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
"use strict";
//----5 глава учебника---------
//alert(prompt('1 number','1')+prompt('2 number','1'));
//----------------
/*function readNumber() {
while (1) {
let a=prompt('input number','');
if (a==='') return null;
if(a==undefined) return null;
if (+a==a) return +a;
}
}
alert(readNumber());*/
//-------------------------- math random integer
/*
function random (min,max) {
return Math.floor(Math.random()*(max-min)+min);
}
let d='';
for(let i=0; i<100;i++){
d+=(random(5,10)+' ; ');
}
alert(d);*/
//-------------------strings
//------ 1 letter to upper case
/*function ucFirst(str) {
if(!str) return 'empty';
str= str[0].toUpperCase()+str.slice(1);
return str;
}
alert(ucFirst('scscsdcsc'));*/
//-------------------- check spam
/*
function checkSpam(str) {
if (str.toLowerCase().includes('viagra')|| str.toLowerCase().includes('xxx')) return true;
else return false;
}
alert(checkSpam('jkshbcas xwlkjexnwqlkjxx xx enxxwwd'));*/
//---------------------truncate усечение строки
/*function truncate(str, maxLength) {
if (str.length>maxLength){
return (str.slice(0,maxLength-1)+'…');
}
return str;
}
alert(truncate('jsahcbsjkhcskdhcb',5));*/
//---------------
/*function extractCurrencyValue(str) {
return +str.slice(1);
}*/
//------------------------ARRAYS
/*
let styles = ['джаз','блюз',];
alert(styles);
styles.push('рокнролл');
alert(styles);
//alert(styles.length/2);
//alert(Math.round(styles.length/2));
styles[Math.floor(styles.length/2)]='классика';
alert(styles);
styles.shift();
alert(styles);
styles.unshift('реп','регги');
alert(styles);
*/
//------------------------sum of array items
/*
function sumInput() {
let arr = [];
while (true) {
let a = prompt('input int', '0');
if (a.trim() === '' || a === null || !isFinite(a)) break;
arr.push(+a);
}
let sum=0;
for (let item of arr) {
sum += item;
alert(item + ' ' + sum);
}
return sum;
}
alert(sumInput());*/
//---------------------- max subArray + начало и конец субмассива с максимальной суммой ---
/*
let arr = [-1, 100, -20, 5, 7, 15, -8, 6, 7, 2, 3, -10, 12, 2, -3,333];
function getMaxSubSum(array) {
let sum = 0;
let maxCurrent = 0;
let max = 0;
let countStart = 0;
let countEnd = 0;
for (let i = 0; i < array.length; i++) {
for (let j = i; j < array.length; j++) {
sum += array[j];
maxCurrent = Math.max(maxCurrent, sum);
if (maxCurrent > max) {
countStart = i;
countEnd = j;
}
max = Math.max(maxCurrent, max);
}
sum = 0;
maxCurrent = 0;
}
alert('maxSum=' + max + ' start ' + countStart + ' fin ' + countEnd);
}
alert(getMaxSubSum(arr));*/