-
Notifications
You must be signed in to change notification settings - Fork 0
/
javaScript.txt
271 lines (212 loc) · 7.37 KB
/
javaScript.txt
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
JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object.
Variables allow computers to store and manipulate data in a dynamic fashion.
They do this by using a "label" to point to the data rather than using the data itself.
Any of the seven data types may be stored in a variable.
All variables and function names are case sensitive. This means that capitalization matters.
function convertToF(celsius) {
var fahrenheit;
// Only change code below this line
fahrenheit = celsius * 9/5 + 32;
// Only change code above this line
return fahrenheit;
}
// Change the inputs below to test your code
convertToF(30);
Here is a table of common escape sequences:
Code Output
\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed
.length
var lastNameLength = 0;
var lastName = "Lovelace";
lastNameLength = lastName.length;
Bracket notation is a way to get a character at a specific index within a string.
Use Bracket Notation to Find the NthtoLast Character in a String
var secondToLastLetterOfLastName = lastName[lastName.length - 2];
Word Blanks game
You will create an (optionally humorous) "Fill in the Blanks" style sentence.
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
var space = " ";
result = "My " + myAdjective + space + myNoun + space + myVerb + space + myAdverb + " on the fence.";
return result;
}
wordBlanks("dog", "big", "ran", "quickly");
Store Multiple Values in one Variable using JavaScript Arrays
var ourArray = ["John", 23];
Nest one Array within Another Array - Multi-dimensional array
var myArray = [["Guitar", 1], ["Dog", 2]];
Modify Array Data With Indexes
var myArray = [1,2,3];
myArray[0] = 3; //[3,2,3]
multi-dimensional array, is as an array of arrays
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
var myData = myArray[2][1];
Append data to the array using push() (End)
var myArray = [["John", 23], ["cat", 2]];
myArray.push(["dog", 3]);
.pop() is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. (End)
var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop(); // [["John", 23]] --> myArray and removedFromArray has ["cat", 2]
.shift() - It works just like .pop(), except it removes the first element instead of the last.
var removedFromMyArray = myArray.shift(); //["John", 23]
.unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array.
var myArray = [["John", 23], ["dog", 3]];
myArray.shift(); //removes John
myArray.unshift(["Paul", 35]); // adds Paul to myArray and becomes Paul, dog
Shopping List
var myList = [];
myList.push(["Milk", 1]);
myList.push(["Roti", 10]);
myList.push(["Butter", 3]);
myList.push(["Chocolate", 2]);
myList.push(["Yogurt", 5]);
we can divide up our code into reusable parts called functions.
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
Add functions
function functionWithArgs(a, b) {
console.log(a + b);
}
functionWithArgs(3,5);
Scope refers to the visibility of variables.Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.
// Declare your variable here
var myGlobal = 10;
function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}
// Only change code above this line
function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal; // 10
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal; //5
}
console.log(output);
}
It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.
Stand in Line (Queue)
Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.
function nextInLine(arr, item) {
// Your code here
//Add the number at the end of the array
arr.push(item);
//Remove the first element of array
var returnItem = arr.shift();
return returnItem; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
IF ELSE
function trueOrFalse(wasThatTrue) {
// Only change code below this line.
if(wasThatTrue){
return "Yes, that was true";
}
return "No, that was false";
// Only change code above this line.
}
// Change this value to test
trueOrFalse(true);
Equality (==) and Strict equality (===)
1 == 1 // true
1 == 2 // false
1 == '1' // true
"3" == 3 // true
Unlike the equality operator, strict equality tests both the data type and value of the compared elements.
3 === 3 // true
3 === '3' // false
10 === 7 //true
Like the equality operator, the inequality (!=) operator will convert data types of values while comparing.
1 != 2 // true
1 != "1" // false
1 != '1' // false
1 != true // false
0 != false // false
The strict inequality operator (!==) is the opposite of the strict equality operator. It means "Strictly Not Equal" and returns false where strict equality would return true and vice versa. Strict inequality will not convert data types.
3 !== 3 // false
3 !== '3' // true
4 !== 3 // true
GOLF GAME
function golfScore(par, strokes) {
// Only change code below this line
if(strokes == 1){
return "Hole-in-one!";
}else if(strokes == par){
return "Par";
}else if(strokes == par-1){
return "Birdie";
}else if(strokes <= par-2){
return "Eagle";
}else if(strokes == par + 1){
return "Bogey";
}else if (strokes == par + 2){
return "Double Bogey";
}else if(strokes >= par + 3){
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(5, 4);
CASE - case values are tested with strict equality (===).
function caseInSwitch(val) {
var answer = "";
switch(val){
case 1: answer = "alpha";
break;
case 2: answer = "beta";
break;
case 3: answer = "gamma";
break;
case 4: answer = "delta";
break;
default: answer = "default";
}
return answer;
}
caseInSwitch(1);
COUNTING CARDS https://en.wikipedia.org/wiki/Card_counting
var count = 0;
function cc(card) {
// Only change code below this line
switch (card){
case 2:
case 3:
case 4:
case 5:
case 6: count++;
break;
case 10 :
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
if (count > 0){
return count + " Bet";
}else{
return count + " Hold";
}
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');