Repository navigation
Expand file tree
/
Copy pathlesson-11.js
More file actions
166 lines (110 loc) · 3.65 KB
/
Copy pathlesson-11.js
File metadata and controls
166 lines (110 loc) · 3.65 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
// Lesson 11
// 1. Counter program in Javascript
// let count = 0;
// document.getElementById("decreaseBtn").onclick = function () {
// count -= 1; // count = count - 1
// document.getElementById("countLabel").innerHTML = count;
// }
// document.getElementById("resetBtn").onclick = function () {
// count = 0;
// document.getElementById("countLabel").innerHTML = count;
// }
// document.getElementById("increaseBtn").onclick = function () {
// count += 1; // count = count + 1
// document.getElementById("countLabel").innerHTML = count;
// }
/*
hmtl
<label class="label" id="countLabel">0</label><br>
<div class="box">
<button id="decreaseBtn">decrease</button>
<button id="resetBtn">reset</button>
<button id="increaseBtn">increase</button>
</div>
css
.box {
display: flex;
gap: 0.5rem;
width: 200px;
margin: 0 auto;
}
.label {
display: block;
font-size: 32px;
font-weight: 700;
text-align: center;
}
*/
// 2. Random number generator in JavaScript
// это генерирует число между 1 и 10, т.е 1 <= a >= 10
// let a = Math.floor(Math.random()*10 + 1);
// это генерирует число между 1 и 6, т.е 1 <= a >= 6
// let b = Math.floor(Math.random()*6 + 1);
// console.log(b);
// let a;
// let b;
// let c;
// document.getElementById("rollButton").onclick = function(){
// a = Math.floor(Math.random() * 6 + 1);
// b = Math.floor(Math.random() * 6 + 1);
// c = Math.floor(Math.random() * 6 + 1);
// document.getElementById("aLabel").innerHTML = a;
// document.getElementById("bLabel").innerHTML = b;
// document.getElementById("cLabel").innerHTML = c;
// }
/* html
<label id="aLabel"></label><br>
<label id="bLabel"></label><br>
<label id="cLabel"></label><br>
<button type="button" id="rollButton">Roll Dice</button>
*/
// 3. JavaScript number guessing game
// const answer = Math.floor(Math.random() * 10 + 1);
// let guesses = 0;
// document.getElementById("submitButton").onclick = function() {
// let guess = document.getElementById("guessField").value;
// guesses += 1 // guesses = guesses + 1
// if(guess == answer){
// alert(`${answer} is the number, it took you ${guesses} guesses!`);
// }
// else if (guess < answer) {
// alert('Too small');
// }
// else {
// alert('Too big');
// }
// }
/* html
<h1>Number Guessing Game</h1>
<p>Pick a number between 1 and 10</p>
<label>Enter a guess</label>
<input type="text" id="guessField">
<button type="button" id="submitButton">Submit</button>
*/
// Useful string methods in JavaScript
// let fullName = 'Sheroz Samatov';
// let phoneNumber = '123-456-7890';
// let methodResult = fullName.length;
// methodResult = fullName.charAt(13);
// methodResult = fullName.indexOf(' ');
// methodResult = fullName.lastIndexOf('o');
// methodResult = fullName.trim();
// methodResult = fullName.toUpperCase();
// methodResult = fullName.toLowerCase();
// console.log(methodResult);
// phoneNumber = phoneNumber.replaceAll('-', '/');
// console.log(phoneNumber);
// String slicing in Javascript
// .slice() - это метод которая вырезает часть текстовой строки и возвращает его как новую строку,
// при этом НЕ меняет исходную строку
// let fullName = 'Sheroz Samatov';
// let firstName;
// let lastName;
// firstName = fullName.slice(0, 6)
// lastName = fullName.slice(7)
// console.log(firstName);
// console.log(lastName);
firstName = fullName.slice(0, fullName.indexOf(" "));
// lastName = fullName.slice(fullName.indexOf(" ") + 1).trim();
// console.log(firstName);
// console.log(lastName);