forked from milq/milq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.js
137 lines (93 loc) · 3.85 KB
/
03.js
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
// CONDITIONALS
'use strict'
// COMPARISON OPERATORS
var a = 5
var b = 3
var z = a === b // Equal to and the same type Result: False
z = a != b // Not equal to or not the same type Result: True
z = a > b // Greater than (type conversion) Result: True
z = a >= b // Greater than or equal to (type conversion) Result: True
z = a < b // Less than (type conversion) Result: False
z = a <= b // Less than or equal to (type conversion) Result: False
// You can compare two strings too. For example, check: 'Patricia' == 'Patricia' --> True
// LOGICAL OPERATORS
var f = false
var t = true
// Operator AND (&&)
z = f && f // Result: False (false && false) --> false
z = f && t // Result: False (false && true) --> false
z = t && f // Result: False (true && false) --> false
z = t && t // Result: True (true && true) --> true
// Operator OR (||)
z = f || f // Result: False (false || false) --> false
z = f || t // Result: True (false || true) --> true
z = t || f // Result: True (true || false) --> true
z = t || t // Result: True (true || true) --> true
// Operator NOT (!)
z = !f // Result: True !(false) --> true
z = !t // Result: False !(true) --> false
// COMBINATION OF COMPARISON AND LOGICAL OPERATORS
a = 5
b = 3
z = !(a == b) || (a >= b && a != b) // Result: True
// DECISIONS WITH IF
var number = 7
var age = 30
var mark = 9.5
var greater_than_zero = number > 0 // Result: true
if (greater_than_zero) { // If statament
console.log('The number is positive.') // Execute this only if variable 'greater_than_zero' is true
}
if (number > 0) { // If statament (popular alternative)
console.log('The number is positive.')
}
if (age >= 18) { // If-else statament
console.log('You are of legal age.')
} else {
console.log('You are not of legal age.')
}
if (mark < 5) { // If-else-if statament
console.log('Fail.')
} else if (mark >= 5 && mark < 7) {
console.log('Pass.')
} else if (mark >= 7 && mark < 9) {
console.log('Remarkable.')
} else {
console.log('Excellent.')
}
// DATA TYPE IDENTIFICATION
var j = 6.37
var k = 'Hello'
var l = true
var m = [3.3, 'World', -1.3, false]
var is_number = typeof(j) === 'number' // Check if variable 'j' is a number. Result: True (False with Objects)
var is_str = typeof(k) === 'string' // Check if variable 'k' is a string. Result: True (False with Objects)
var is_bool = typeof(l) === 'boolean' // Check if variable 'l' is a boolean. Result: True (False with Objects)
var is_array = Array.isArray(m) // Check if variable 'm' is an array. Result: True
// EXAMPLE 1 (NESTED IFS): program to check if the input year is a leap year or not
var year = 2000
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
console.log(year, 'is a leap year.')
} else {
console.log(year, 'is not a leap year.')
}
} else {
console.log(year, 'is a leap year.')
}
} else {
console.log(year, 'is not a leap year.')
}
// EXAMPLE 2: converter from Fahrenheit to Celsius and vice versa
var temperature = '113.0F'
var unit = temperature.slice(-1)
var value = Number(temperature.slice(0, -1))
if (unit == 'C' || unit == 'c') {
var fahrenheit = (value * 1.8) + 32
console.log(String(fahrenheit) + ' ºF')
}
if (unit == 'F' || unit == 'f') {
var celcius = (value - 32) / 1.8
console.log(String(celcius) + ' ºC')
}