-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
174 lines (131 loc) · 3.58 KB
/
practice.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
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
// var message = "Hello, world!";
// console.log(message);
// JavaScript is an untyped language.
// var x = "Hey!";
// var y = 10;
// console.log(x);
// console.log(y);
// console.log(x + y);
// y = " is for Horses!";
// console.log(x + y);
// Declaring multiple variables in one line
// var x = 1, y = 2, z = "3";
// console.log(x + y + z);
// Data types
// numbers
// var integer_num = 1;
// var float_num = 1.23;
// console.log(integer_num + float_num);
// // strings
// var message = "Let's learn JavaScript!\n";
// console.log(message);
// // booleans
// var is_cool = true;
// console.log(is_cool + ' dat!');
// // arrays
// var my_stuff = [integer_num, message, is_cool];
// console.log(my_stuff);
// hashes - no symbols as keys, only strings
// var capitals = {
// "LA": "Baton Rouge",
// "TX": "Austin",
// "GA": "Atlanta"
// };
// console.log(capitals["LA"]);
// Null and Undefined
// var x;
// // Variables that have not been initialized with a value are undefined:
// console.log(x); // undefined
// // But they are not null:
// console.log(x === null); // false
// // Unless you make them null:
// x = null;
// console.log(x); // null
// console.log(x === null); // true
// console.log(x === undefined); // false
// // Or use type coercion - 2 equal signs will change the value rather than simply compare
// console.log(x == undefined); // true
// Scope
// This variable is in the Global scope, on purpose:
// var x = "I'm a global variable called x!";
// // console.log(x);
// // Define a function called someFunction:
// function someFunction(){
// // This variable only exists inside the function (aka local):
// var y = "I'm a local variable called y!";
// // console.log(x);
// // console.log(y);
// // This is automatically global because we forgot the "var". Don't do it.
// z = 10;
// console.log(z);
// }
// someFunction();
// // console.log(x);
// // console.log(y); // results in an error
// console.log(z);
// Operators
// var x = 10,
// y = 5,
// z = 3;
// console.log(x + y); // 15
// console.log(x - y); // 5
// console.log(x * y); // 50
// console.log(x / y); // 2
// console.log(Math.floor(x / z)); // 3 Used Math.floor to round down (not like Ruby integer math)
// console.log(x % z); // 1 modulus, or remainder
// console.log(x > y);
// console.log(x < y);
// console.log(x >= y);
// console.log(x <= y);
// console.log(x === y);
// console.log(x !== y);
// var a = true, b = false;
// console.log(a && b); // AND false
// console.log(a || b); // OR true
// Conditionals
// var x = 10, y = "popcorn";
// if (x > y){
// console.log("x is greater than y");
// } else if (x < y){
// console.log("x is less than y");
// } else {
// console.log("x is neither greater than or less than y");
// }
// Loops
// var x = [1, 2, 3, 4, 5];
// // C-style loop
// for (var i = 0; i < x.length; i++){
// console.log(x[i]);
// }
// // For each
// x.forEach(function (element){
// console.log(element);
// });
// // While loop
// var i = 0;
// while (i < x.length){
// console.log(x[i]);
// i++;
// }
// A function with explicit arguments
// function sumExplicitly(a, b){
// console.log(a + b);
// }
// sumExplicitly(10, 5);
// // A function with implicit arguments
// function sumImplicitly(){
// var total = 0, i;
// for (i = 0; i < arguments.length; i++){
// total += arguments[i];
// }
// console.log(total);
// }
// sumImplicitly(1, 4, 7, 3, 20);
// We must use "return" to actually return a value
function sum(a, b){
return a + b;
}
var x = sum(10, 5),
y = sum(x, sum(20, 30));
console.log(x);
console.log(y);