-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.js
More file actions
303 lines (239 loc) · 5.69 KB
/
tutorial.js
File metadata and controls
303 lines (239 loc) · 5.69 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
// JavaScript Basics Tutorial
// Single line comment
/*
Multi line comment
*/
// Variables
var myName;
var a = 9;
var b;
b = a;
var c;
var camelCase;
var PascalCase;
// Increment
console.log(a); // 9
a++;
console.log(a); // 10
// Decrement
console.log(b); // 9
b--;
console.log(b); // 8
// Strings
var doubleQuotes = "Quotes \"within\" quotes";
var singleQuotes = 'Quotes "within" quotes';
var escapeChars = 'First\nSecond\n\t\\Third';
var concatString = 'First part and ' + ' the Second part';
var anotherString = "This is the beginning. ";
anotherString += "The End.";
var aWord = "Something";
var firstLetter = aWord[0];
var lastLetter = aWord[aWord.length - 1];
// Arrays
var myArray = []; // An empty array
// Function
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "There was a " + myAdjective + " " + myNoun + " that " + myAdverb + " " + myVerb + ".";
return result;
}
wordBlanks("dog", "big", "ran", "quickly");
var numArr = [21, 55, 9999]; // Array with numbers
var firstNum = numArr[0]; // Assigning the value of the 0 index of numArr to a variable
var multiDimensional = [['arr1-1', 1], ['arr1-2', 2]];
function myFunction() {
console.log("This is my function.");
}
myFunction(); // This is my function.
// Passing an argument to a function
function functionWithArg(arg) {
console.log("This function accepts an arg. The arg is: " + arg);
}
functionWithArg(5); // This function accepts an arg. The arg is: 5
functionWithArg("Something"); // This function accepts an arg. The arg is: Something
function trueOrFalse(bool) {
if (bool) {
return "It's true";
}
return "False."
}
function testEqual(num) {
if(num == 10) {
return "num equals 10";
}
return "num does not equal 10";
}
console.log(testEqual(5)); // num does not equal 10
console.log(testEqual(10)); // num equals 10
function testNotEqual(str) {
if(str != "Bad string") {
return "This is fine.";
}
return "This is not fine.";
}
console.log(testNotEqual("Okay.")); // This is fine.
console.log(testNotEqual("Bad string")); // This is not fine.
function whatIsMyFunction(func) {
if(func == "You pass butter") {
console.log("Oh my god...");
} else {
console.log("Does not compute.");
}
}
whatIsMyFunction("You pass butter"); // Oh my god...
whatIsMyFunction("Error"); // Does not compute.
// Comparative operators
function howManyEggs(eggs) {
if(eggs < 12) {
console.log("This is not enough eggs...");
} elif (eggs > 12) {
console.log("This is too many eggs...");
} else {
console.log("Perfect.");
}
}
howManyEggs(4); // This is not enough eggs...
howManyEggs(200); // This is too many eggs...
howManyEggs(12); // Perfect.
// The && and || Operators
function andOperator(this, that) {
if(this && that) {
console.log("Everything checks out.");
} elif(this || that) {
console.log("Half right.");
} else {
console.log("Something is missing.");
}
}
andOperator(true, true); // Everything checks out.
andOperator(false, false); // Something is missing.
andOperator(true, false); // Half right.
// Golf
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
if(strokes == 1) {
return names[0];
} else if (strokes <= par - 2) {
return names[1];
} else if (strokes == par - 1) {
return names[2];
} else if (strokes == par) {
return names[3];
} else if (strokes == par + 1) {
return names[4];
} else if (strokes == par + 2) {
return names[5];
} else {
return names[6]
}
}
golfScore(5, 4); // Birdie
golfScore(4, 4);
// Switch Statement
function caseInSwitch(val) {
var answer = "";
switch(val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "delta";
break;
case 4:
answer = "gamma";
break;
case 5:
case 6:
default:
answer = "invalid";
break;
}
return answer;
}
caseInSwitch(1); // alpha
// Returning booleans
function isLess(a, b) {
return a < b;
}
isLess(3, 4); // true
isLess(99999, 1); // false
function abTest(a, b) {
if(a < 0 || b < 0)
return undefined;
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
console.log(abTest(2,2)); // 8
// Card Counter
var count = 0;
function cc(card) {
var decision = '';
switch(card) {
case 2: case 3: case 4: case 5: case 6:
count++;
break;
case 7: case 8: case 9:
break;
case 10: case 'J': case 'Q': case 'K': case 'A':
count--;
break;
default:
break;
}
if(count > 0) {
decision = 'Bet';
} else {
decision = 'Hold';
}
return count + ' ' + decision;
}
cc(2); cc(3); console.log(cc(7)); cc('K'); cc('A'); // will finally return 0 Hold
// Objects
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
var myDog = {
"name": "Miles",
"legs": 4,
"tails": 2,
"friends": ["Sanic"]
};
dogName = myDog.name;
console.log(dogName); // Miles
// Object hasOwnProperty method
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
return myObj.hasOwnProperty(checkProp) ? myObj[checkProp] : "Not Found";
}
console.log(checkObj("candy"));
console.log(checkObj("bed"));
console.log(checkObj("pet"));
console.log(checkObj("potato"));
console.log(checkObj("gift"));
// While Loop
var myArr = [];
var i = 0;
while(i < 5) {
myArr.push(i);
i++;
}
console.log(myArr); // [0, 1, 2, 3, 4]
// For Loop
var loopArr = [];
for(var j = 1; j < 6; j++) {
loopArr.push(j);
}
console.log(loopArr); // [1, 2, 3, 4, 5]
for(var k = 5; k > 0; k--) {
loopArr.pop();
}
console.log(loopArr); // []
console.log();