-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoloLearn_JavaScript.html
493 lines (457 loc) · 16.5 KB
/
SoloLearn_JavaScript.html
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE html>
<!--
## GitHub: dark-teal-coder
## First Published Date: 2022-02-07
## Program Input(s):
### (1) an integer representing the well's depth
## Program Process(es):
### (1) using the while loop to calculate the number of days it takes for the snail to climb up the well of an input depth
## Program Output(s):
### (1) an integer representing the number of days it takes for the snail to reach the depth
#3 Program Description: This program is a solution to the problem "The Snail in the Well" in 23 Code Project in the JavaScript course on SoloLearn.
-->
<!------------------------------------------------------------------------------------------------->
<html>
<!--
JavaScript code lives inside the HTML document and needs to be enclosed by <script> and </script>.
We can also use an external JavaScript file using an URL:
<script src="https://gist.github.com/dark-teal-coder/fcab679cd11c92dfee7b886c4d1a5ddb.js"></script>
Or, using the file name:
<script src="file.js"></script>
-->
<script>
// document.write() is used to write text into our HTML document.
// We can use standard HTML language to customize the appearance of text in the output.
document.write("<h1>Hello, World!</h1>");
// console.log() is used to output text to the browser console.
// The console is part of the web browser and allows you to log messages, run JavaScript code, and see errors and warnings.
console.log("Text here will be output to the browser console.");
// Variables are containers for storing data values.
// Use the keyword var to declare a variable.
// Use the assignment operator (=) to assign a value to a variable.
// JavaScript is case sensitive.
var x = 10;
document.write(x);
x = 20;
document.write(x);
// JavaScript statements are separated by (;).
// JavaScript variable names are case-sensitive.
// Naming rules:
// (1) The 1st character of a variable name must be a letter, (_), or ($); subsequent characters can be letters, digits, (_), or ($).
// (2) The 1st character can't be a number.
// (3) Variable names can’t include a mathematical or logical operator in their name.
// (4) Variable names can’t contain spaces.
// (5) Any special symbols aren't allowed.
// Statements: the instructions within our program that get executed when the program runs
// Any code after (//), or between (/*) and (*/) is treated as a comment, and will not be executed.
// This is a single line comment.
/* This is a multi-line comment.
This is a multi-line comment. */
// alert() is used to create a message box.
alert("This is an alert box!");
// Comments make our code more readable for others.
// Data type refers to the types of values a program can work with.
// (1) Numer without decimals
var integer = 10;
// (2) Numer with decimals
var decimal = 9.99;
// (3) Strings
// A string can be any text wrapped in single or double quotes.
var singleString = 'Ree';
var doubleString = "My name is Ree.";
var quotesString = "My name is 'John' ";
// To get single/double quotes inside of single/double quotes, use the escape character (\).
var sayHello = 'Hi, there! I\'m a JavaScript programmer.';
document.write(sayHello);
// Special characters
// (1) Single quote \'
// (2) Double quote \"
// (3) Backslash \\
// (4) New line \n
// (5) Carriage return \r
// (6) Tab \t
// (7) Backspace \b
// (8) Form feed \f
// Booleans: (1) true (2) false
var isFemale = true;
var isMale = false;
// The Boolean value of 0 (zero), null, undefined, empty string is false.
// Arithmetic operators
// (1) Addition (+)
// (2) Subtraction (-)
// (3) Multiplication (*)
// (4) Division (/)
// (5) Modulus (%)
// (6) Increment (++)
// (7) Decrement (--)
// Addition and Subtraction
document.write(1 + 2 - 3);
// eval() is used to get the result of a string expression.
document.write(eval("2 * 3 + 4"));
// Multiplication
document.write(10 * 5); // Output: 50
// (10 * '5') or ('10' * '5') will give the same result.
document.write(10 * '5'); // Output: 50
document.write('10' * '5'); // Output: 50
// But trying to multiply a number with string values that aren’t numbers will return NaN (Not a Number).
document.write('Ree' * '5'); // Output: NaN
// Division
document.write(10 * 3); // Output: 3.3333333333333335
// The Modulus
document.write(10 % 3); // Output: 1
// We can use the modulus operator on integers and on floating point numbers.
// Increment & Decrement
// The increment/decrement operator increases/decreases the numeric value of its operand by 1.
// (1) Post Increment var++
var a = 0, b = 10;
var a = b++;
document.write(a) // Output: 10
document.write(b) // Output: 11
// (2) Pre Increment ++var
var a = 0, b = 10;
var a = ++b;
document.write(a) // Output: 11
document.write(b) // Output: 11
// (3) Post Increment var--
var a = 0, b = 10;
var a = b--;
document.write(a) // Output: 10
document.write(b) // Output: 9
// (4) Pre Increment --var
var a = 0, b = 10;
var a = --b;
document.write(a) // Output: 9
document.write(b) // Output: 9
// Assignment Operators
// (1) Operator =
// x = y equivalent to x = y
// (2) Operator +=
// x += y equivalent to x = x + y
// (3) Operator -=
// x -= y equivalent to x = x - y
// (4) Operator *=
// x *= y equivalent to x = x * y
// (5) Operator /=
// x /= y equivalent to x = x / y
// (6) Operator %=
// x %= y equivalent to x = x % y
// Comparison Operators
// Use comparison operators in logical statements to find out if variables or values are different.
// You get either true or false.
// (1) Equal to (==) operator
console.log(1 == 0); // Output: false
console.log(1 == '1'); // Output: true
// (2) Identical (===) operator (equal and of same type)
console.log(1 === 0); // Output: false
console.log(1 === '1'); // Output: false
// (3) Not equal to (!=) operator
console.log(1 != 0); // Output: true
console.log(1 != '1'); // Output: false
// (4) Not identical (!==) operator
console.log(1 !== 0); // Output: true
console.log(1 !== '1'); // Output: true
// (5) Greater than (>) operator
console.log(1 > 0); // Output: true
console.log(1 > 0); // Output: true
// (6) Greater than or equal to (>=) operator
console.log(1 >= 0); // Output: true
console.log(1 >= 0); // Output: true
// (7) Less than (<) operator
console.log(1 < 0); // Output: false
console.log(1 < 0); // Output: false
// (8) Less than or equal to (<=) operator
console.log(1 <= 0); // Output: false
console.log(1 <= 0); // Output: false
// Logical/Boolean Operators
// Logical operators evaluate an expression and return true or false.
// (1) AND Operator (&&) returns true if both operands are true.
// (2) OR Operator (||) returns true if one operand is true.
// (3) NOT Operator (!) returns true if the operand is false, and false if the operand is true.
document.write("AND Operator");
document.write(true && true); // Output: true
document.write(true && false); // Output: false
document.write(false && true); // Output: false
document.write(false && false); // Output: false
document.write("OR Operator");
document.write(true || true); // Output: true
document.write(true || false); // Output: true
document.write(false || true); // Output: true
document.write(false || false); // Output: false
document.write("NOT Operator");
document.write(! true); // Output: false
document.write(! false); // Output: true
// Use parentheses to group an expression
document.write((1 > 0) && (0 < 1)); // Output: true
// Conditional/Ternary Operator
// Conditional operators assign a value to a variable, based on some condition.
// Syntax: variable = (condition) ? value1: value2
var age = 30;
var isAdult = (age < 18) ? "Too young": "Old enough";
document.write(isAdult);
// String Operators
// Use concatenation (+) to build strings made up of multiple smaller strings.
document.write("GitHub: " + "dark-teal-coder");
document.write("Free programming school: " + "42" + "Bangkok");
// Conditional statements
// The "if" Statement
// Use if to specify a block of code that we want to be executed if a specified condition is true.
// Syntax:
// if (condition) {
// // statements
// }
var myNum1 = 7;
var myNum2 = 10;
if (myNum1 < myNum2) {
alert("JavaScript is easy to learn.");
}
// The "else" Statement
// Use the else statement to specify a block of code that will execute if the condition is false.
// Syntax:
// if (expression) {
// // executed if condition is true
// }
// else {
// // executed if condition is false
// }
var myNum1 = 7;
var myNum2 = 10;
if (myNum1 > myNum2) {
alert("This is my 1st condition");
}
else {
alert("This is my 2nd condition");
}
// Alternative: a > b ? alert(a) : alert(b)
// The "else if" Statement
// The else-if statement is useful because it lets us specify a new condition if the first condition is false.
var course = 1;
if (course == 1) {
document.write("<h1>HTML Tutorial</h1>");
} else if (course == 2) {
document.write("<h1>CSS Tutorial</h1>");
} else {
document.write("<h1>JavaScript Tutorial</h1>");
}
// The final else block will be executed when none of the conditions is true.
// We can write as many else if statements as you need.
// The "switch" Statement
// We can use the switch statement to perform different actions based on different conditions.
// Syntax:
// switch (expression) {
// case n1:
// statements
// break;
// case n2:
// statements
// break;
// default:
// statements
// }
// The value of the expression is compared with the values of each case, and if there's a match, that block of code is executed.
// The switch statement is more effective than multiple if-else statements.
var day = 2;
switch (day) {
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
// We can have as many case statements as you need.
// The "break" Keyword
// Breaking out of the switch block stops the execution of more code and case testing inside the block.
// Usually, a break should be put in each case statement.
// The default keyword specifies the code to run if there's no case match.
var color ="yellow";
switch (color) {
case "blue":
document.write("This is blue.");
break;
case "red":
document.write("This is red.");
break;
case "green":
document.write("This is green.");
break;
default:
document.write("Color not found.");
}
// The default block can be omitted, if there is no need to handle the case when no match is found.
// Loops
// Loops can execute a block of code a number of times.
// JavaScript has three types of loops: (1) for (2) while (3) do while
// The "for" Loop
// Syntax:
// for (statement 1; statement 2; statement 3) {
// // code block to be executed
// }
// Statement 1 is executed before the code block starts.
// Statement 2 defines the condition for running the code block.
// Statement 3 is executed each time after the code block has been executed.
for (i = 1; i <= 5; i++) {
document.write(i + "<br />");
}
// Statement 1 is optional, and can be left out, if your values are set before the loop starts.
var i = 1;
for (; i <= 5; i++) {
document.write(i + "<br />");
}
// We can also initiate more than one value in statement 1, using commas to separate them.
for (i = 1, text = ""; i <= 5; i++) {
text = i;
document.write(i + "<br />");
}
// If statement 2 returns true, the loop will start over again. If it returns false, the loop will end.
// Statement 2 is also optional only if you put a break inside the loop.
// Statement 3 is used to change the initial variable. It can do anything, including negative increment (i--), positive increment (i = i + 15).
// Statement 3 is also optional only if you increment your values inside the loop.
var i = 0;
for (; i < 10; ) {
document.write(i);
i++;
}
// The "while" Loop
// The while loop repeats through a block of code, but only as long as a specified condition is true.
// Syntax:
// while (condition) {
// // code block
// }
var i = 0;
while (i <= 10) {
document.write(i + "<br />");
i++;
}
// Avoid infinite loop: make sure that the condition in a while loop eventually becomes false.
// The "do-while" Loop
// The "do-while" loop will execute the code block once before checking if the condition is true, and then it will repeat the loop as long as the condition is true.
// Syntax:
// do {
// // code block
// }
// while (condition);
var i = 20;
do {
document.write(i + "<br />");
i++;
}
while (i <= 25);
// The loop will always be executed at least once even if the condition is false.
// The "break" and "continue" Statements
// The "break" statement ends the execution of the loop.
for (i = 0; i <= 10; i++) {
if (i == 5) {
break;
}
document.write(i + "<br />");
}
// The "continue" statement breaks only one iteration in the loop, and continues with the next iteration.
var sum = 0;
for (i = 4; i < 8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum); // output: 16
// JavaScript Functions
// A function is a block of code designed to perform a particular task.
// Code reuse: Define the code once, and use the same code many times with different arguments, to produce different results.
// Syntax:
/*
function functionName() {
// code
}
*/
// Function naming rules are the same as the variable ones.
// Invoke/call a function to execute it.
function myFunction() {
alert("Calling a Function!");
}
myFunction();
// Function Parameters
// Function parameters are the names listed in the function's definition.
// Syntax:
/*
function functionName(param1, param2, param3) {
// code
}
*/
// We can use parameters inside the function.
// When calling the function, provide the parameter's value (argument) inside ().
// We can define one function, and pass different arguments to it.
function sayHello(name) {
alert("Hi, " + name);
}
sayHello("David");
sayHello("Sarah");
sayHello("John");
// Multiple Parameters
function test(x, y) {
if(x > y) {
document.write(x);
}
else {
document.write(y);
}
}
test(5, 8);
// Provide the arguments in the same order in which you defined them.
function sayHello(name, age) {
document.write( name + " is " + age + " years old.");
}
sayHello("John", 20);
// If a function is called with missing arguments (fewer than declared), the missing values are set to undefined, which indicates that a variable has not been assigned a value.
// Function Return
// Use the (optional) return statement to return a value from a function.
// The function stops executing when reaching a return statement.
function myFunction(a, b) {
return a * b;
}
var x = myFunction(5, 6);
document.write(x);
// A function will return undefined if nothing is returned.
function addNumbers(a, b) {
var c = a + b;
return c;
}
document.write(addNumbers(40, 2));
// 3 types of popup boxes: the Alert, Prompt, and Confirm boxes.
// Alert Box
// The alert function takes 1 parameter, which is the text displayed in the popup box.
// The user must click OK to proceed.
alert("Do you really want to leave this page?");
// Use \n to display line breaks within a popup box.
// Prompt Box
// A prompt box is often used to have the user input a value before entering a page.
// The user must click either OK or Cancel to proceed after entering the input value.
// OK returns the input value. Cancel returns null.
// The prompt() method takes 2 parameters:
// (1) the label to display in the text box
// (2) the default string to display in the text box (optional)
var user = prompt("Please enter your name: ");
alert("Hi, " + user + "!");
// Confirm Box
// A confirm box is often used to have the user verify or accept something.
// The user must click either OK or Cancel to proceed.
// OK returns true. Cancel returns false.
var result = confirm("Do you really want to leave this page?");
if (result == true) {
alert("Thanks for visiting!");
}
else {
alert("Thanks for staying with us!");
}
//
</script>
<head>
<title>GitHub: DarkTealCoder</title>
</head>
<body>
</body>
</html>