-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
475 lines (376 loc) · 10.1 KB
/
Copy pathindex.js
File metadata and controls
475 lines (376 loc) · 10.1 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
console.log("I like books!");
console.log("Hello everyone!");
//window.alert("I really love to read books!!!");
//This is a comment
/*
This is a multiline comment
*/
// Variables
let firstName = "Samra";
let age = 21;
let student = true;
console.log("Hello", firstName);
console.log("You are", age, "years old");
console.log("Enrolled:", student);
document.getElementById("p1").innerHTML = "Hello " + firstName;
document.getElementById("p2").innerHTML = "You are " + age + " years old";
//Arithmetic expressions
let studentsArithmetic = 20;
//studentsArithmetic = studentsArithmetic + 1;
//studentsArithmetic = studentsArithmetic - 1;
//studentsArithmetic = studentsArithmetic * 2;
//studentsArithmetic = studentsArithmetic / 2;
//let extraStudent = studentsArithmetic % 2;
//studentsArithmetic += 1;
//studentsArithmetic -= 1;
//studentsArithmetic *= 2;
//studentsArithmetic /= 2;
//console.log(extraStudent);
console.log(studentsArithmetic);
/* operator precedence */
let result = (1 + 2) * (3 + 4);
console.log(result);
//User input
// Easy way with a window prompt
//let username = window.prompt("What's your name?");
//console.log(username);
// Difficult way HTML textbox
let username;
document.getElementById("myButton").onclick = function() {
username = document.getElementById("myText").value;
console.log(username);
document.getElementById("myLabel").innerHTML = "Hello " + username;
}
//Type conversion
/*let ageConversion = window.prompt("How old are you?");
console.log(typeof ageConversion);
ageConversion = Number(ageConversion);
console.log(typeof ageConversion);
ageConversion += 1;
console.log("Happy Birthday! You are", ageConversion, "years old");*/
let x;
let y;
let z;
x = Number("3.14");
y = String(3.14);
z = Boolean("pizza");
console.log(x, typeof x);
console.log(y, typeof y);
console.log(z, typeof z);
// const
const PI = 3.14159;
let radius;
let circumference;
//radius = window.prompt("Enter the radius of a circle");
radius = Number(radius);
//PI = 420.69;
circumference = 2 * PI * radius;
console.log("The circumference is:", circumference);
// Math
let p = 3.71;
p = Math.round(p);
console.log(p);
let n = 3.14;
let l = 5;
let v = 9;
let maximum;
let minimun;
//n = Math.floor(n);
//n = Math.ceil(n);
//n = Math.pow(n, 2);
//n = Math.sqrt(n);
//n = Math.abs(n);
//maximum = Math.max(n, l, v);
//minimum = Math.min(n, l, v);
n = Math.PI;
console.log(n);
// Hypotenuse calc practice program
/* let a;
let b;
let c;
a = window.prompt("Enter side A");
a = Number(a);
b = window.prompt("Enter side B");
b = Number(b);
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
console.log("Side C:", c); */
document.getElementById("submitButton").onclick = function() {
a = document.getElementById("aTextBox").value;
a = Number(a);
b = document.getElementById("bTextBox").value;
b = Number(b);
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
document.getElementById("cLabel").innerHTML = "Side C: " + c;
}
//count
let count = 0;
document.getElementById("decreaseBtn").onclick = function() {
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;
document.getElementById("countLabel").innerHTML = count;
}
// Random number generator
let u = Math.floor(Math.random() * 6) + 1;
let w = Math.floor(Math.random() * 6) + 1;
let t = Math.floor(Math.random() * 6) + 1;
document.getElementById("rollButton").onclick = function() {
u = Math.floor(Math.random() * 6) + 1;
w = Math.floor(Math.random() * 6) + 1;
t = Math.floor(Math.random() * 6) + 1;
document.getElementById("uLabel").innerHTML = u;
document.getElementById("wLabel").innerHTML = w;
document.getElementById("tLabel").innerHTML = t;
}
// Useful string methods
let userName = "Samra Raković";
let phoneNumber = "123-456-7890";
//console.log(userName.length);
//console.log(userName.charAt(0));
//console.log(userName.indexOf("o"));
//console.log(userName.lastIndexOf("a"));
//userName = userName.trim();
//userName = userName.toUpperCase();
//userName = userName.toLowerCase();
phoneNumber = phoneNumber.replaceAll("-", "/");
console.log(userName);
console.log(phoneNumber);
// String slicing
let fullName = "Samra Raković";
let fName;
let lastName;
//fName = fullName.slice(0, 5);
//lastName = fullName.slice(6);
fName = fullName.slice(0, fullName.indexOf(" "));
lastName = fullName.slice(fullName.indexOf(" ") + 1);
console.log(fName);
console.log(lastName);
// Method chaining
let uName = "samra";
let letter = uName.charAt(0).toUpperCase().trim();
console.log(letter);
// if statement
/* let ageN = 75;
if(ageN >= 65){
console.log("You are a senior citizen!");
}
else if(ageN >= 18){
console.log("You are an adult!");
}
else if (ageN < 0){
console.log("YOU HAVEN'T BEEN BORN YET!");
}
else{
console.log("You are a child!");
} */
let online = false;
if(online){
console.log("You are online!");
}
else{
console.log("You are offline!");
}
// checked propery
document.getElementById("myButton1").onclick = function() {
const myCheckBox = document.getElementById("myCheckBox");
const visaBtn = document.getElementById("visaBtn");
const mastercardBtn = document.getElementById("mastercardBtn");
const paypalBtn = document.getElementById("paypalBtn");
if(myCheckBox.checked) {
console.log("You are subscribed!");
}
else{
console.log("You are not subscribed!");
}
if(visaBtn.checked){
console.log("You are paying with a Visa!");
}
else if(mastercardBtn.checked){
console.log("You are paying with a Mastercard!");
}
else if(paypalBtn.checked){
console.log("You are paying with a Paypal!");
}
else{
console.log("You must select a payment type!");
}
}
// Switch
/* let grade = "Pizza";
switch(grade) {
case "A":
console.log("You did great!");
break;
case "B":
console.log("You did good!");
break;
case "C":
console.log("You did okay!");
break;
case "D":
console.log("You passed .... barely");
break;
case "F":
console.log("You FAILED!");
break;
default:
console.log(grade, "is not a letter grade");
} */
let grade = 61;
switch(true) {
case grade >= 90:
console.log("You did great!");
break;
case grade >= 80:
console.log("You did good!");
break;
case grade >= 70:
console.log("You did okay!");
break;
case grade >= 60:
console.log("You passed .... barely");
break;
case grade > 60:
console.log("You FAILED!");
break;
default:
console.log(grade, "is not a letter grade");
}
// AND OR logical operators
/* let temp = 50;
if(temp > 0 && temp < 30) {
console.log("The weather is good!");
}
else {
console.log("The weather is bad!");
} */
/* let temp = -10;
if(temp <= 0 || temp >= 30) {
console.log('The weather is bad!');
}
else {
console.log('The weather is good!');
} */
let temp = 15;
let sunny = false;
if(temp > 0 && temp < 30 && sunny) {
console.log('The weather is good!');
}
else {
console.log('The weather is bad!');
}
// NOT logical operator
let tempO = 15;
let sunnyI = false;
if(!(tempO > 0)){
console.log("It's cold outside");
}
else{
console.log("It's warm outside");
}
if(!sunnyI) {
console.log("It's cloudy outside");
}
else{
console.log("It's sunny outside");
}
// While loop
/* let userNameN = "";
while(userNameN == "" || userNameN == null) {
userNameN = window.prompt("Enter your name");
}
console.log("Hello", userNameN); */
/* while( 1 == 1){
console.log("HELP! I'M STUCK IN AN INFINITE LOOP!");
} */
// do while loop
/* let userNameL;
do{
userNameL = window.prompt("Enter your name");
}while(userNameL == "")
console.log("Hello", userNameL) */
// for loop
/* for(let counter = 50; counter <= 100; counter += 1) {
console.log(counter);
} */
/* for(let i = 1; i <= 10; i += 1) {
console.log(i);
} */
/* for(let i = 10; i > 0; i -= 3) {
console.log(i);
}
console.log("HAPPY NEW YEAR!"); */
// break and continue statements
/* for(let i = 1; i <= 20; i += 1) {
if(i == 13) {
continue;
}
console.log(i);
} */
// nested loop
/* let symbol = window.prompt("Enter a symbol to use");
let rows = window.prompt('Enter # of rows');
let columns = window.prompt('Enter # of columns');
for(let i = 1; i <= rows; i += 1) {
for(let j = 1; j <= columns; j += 1) {
document.getElementById("myRectangle").innerHTML += symbol;
}
document.getElementById("myRectangle").innerHTML += "<br>";
} */
// functions
startProgram();
function startProgram(){
let userNameQ = "Samra";
let ageG = 27;
happyBirthday(userNameQ, ageG);
}
function happyBirthday(b, a){
console.log("Happy Birthday to you!");
console.log("Happy Birthday to you!");
console.log("Happy Birthday dear", a);
console.log("Happy Birthday to you!");
console.log("You are", b," years old!");
}
// return statement
/* let area;
let width;
let height;
width = window.prompt("Enter width");
height = window.prompt("Enter height");
area = getArea(width, height);
console.log("The area is:", area)
function getArea(width, height){
return width * height;
} */
// ternary operator
/* let adult = checkAge(15);
console.log(adult);
function checkAge(age){
return age >= 18 ? true : false;
} */
checkWinner(true);
function checkWinner(win){
win ? console.log('YOU WIN!') : console.log('YOU LOSE!');
}
// var vs let
/* doSomething();
function doSomething(){
for(var i = 1; i <= 3; i += 1){
// console.log(i);
}
}
console.log(i); */
let name = "Samra";
// Template literals
let userNameX = "Samra";
let items = 5;
let total = 75;
/* console.log("Hello", userNameX);
console.log("You have", items, "items in your cart");
console.log("Your total is $", total); */