-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab.js
More file actions
324 lines (217 loc) · 7.21 KB
/
Copy pathlab.js
File metadata and controls
324 lines (217 loc) · 7.21 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// JavaScript Review Lab
// Working as a team, replace the null values
// with JavaScript that completes the task.
//
// For each challenge, copy and paste it into
// a browser's console. Make it work inside
// the console. Then, put the working version
// into this file!
/************************
* BASICS
************************/
/* 1. Declare variables that are examples of the
five primitive types. For example, assign
true to booleanValue, since true is a boolean
value. Show two different ways to make a
variable undefined! */
var booleanValue = true;
var numberValue = 1;
var stringValue = 'hello';
var nullValue = null;
var undefinedValue;
var undefinedValue2 = undefined;
/* 2. In your console, use the typeof operator on
each of the above variables. For example: */
typeof booleanValue
/* 3. Which of the above variables does typeof NOT
show the expected primitive type? This is
a bug in JavaScript! It has not been fixed
since a lot of code online depends on it. */
/* Mia, Albert, and Tony are going to a party.
Set "attendees" to an array of their names. */
attendees = ['Mia','Albert','Tony'];
/* Create the same array in a different way --
by creating a new Array object. */
var attendees = new Array('Mia', 'Albert', 'Tony');
/* Access the third element in the array. */
attendees[2];
/* Write a statement which sorts the array. */
attendees.sort()
/* Write a statement which removes the last
element of the array. */
attendees.pop()
/* Write a statement which shifts a new element
into the array as element 0 -- "Elaine" */
attendees.unshift('Elaine')
/* console.log the following poem. Do it using
a single console.log statement! Make sure there
are four separate lines in the console output.
Computers are fun,
Because they are neat.
Javascript and Ruby,
They cannot be beat! */
console.log("Computers are fun,\nBecause they are neat.\nJavascript and Ruby,\nThey cannot be beat!");
/************************
* WHILE LOOPS
************************/
/* Fix the below while loop so that the user
is continually asked whether he or she is done.
Continue looping while the user enters "no".
Remember you can place console.log statements
inside loops to "see" the values change. */
var isDone = "no";
while (isDone === "no") {
isDone = prompt("Are you done?");
}
/* Modify the solution to the above while loop
below. Now, continue looping if "no" OR "No"
is entered. */
var isDone = "no";
while (isDone === "no" || isDone === "No") {
isDone = prompt("Are you done?");
}
/* Explain to each other in English what this statement
does. It should be a simple statement.
Look up functions such as Math.round() and
Math.random() if they are new. Try pasting the
individual parts of this statement into
your console to better understand it! */
var theTarget = Math.round(100 * Math.random()) + 1;
Multiply a randomly generated number by 100 and add 1 to it. Assign the result of the expression to variable theTarget.
/* Number guessing game. While the guess is not
the target value, continue asking the user
for a guess and informing whether the guess
is too low or too high. */
var guess = 0;
var theTarget = Math.round(100 * Math.random()) + 1;
while (guess != theTarget) {
guess = prompt("Make a guess!");
if (guess > theTarget) {
alert("Too high!");
} else if (guess < theTarget) {
alert("Too low!");
}
}
/* The following while loop implements a
"count up" clock. Make it console.log the
numbers 1, 2, 3, 4, and 5. */
var i = 0;
while (i < 5) {
console.log(++i);
}
/************************
* FOR LOOPS
************************/
/* Rewrite the "count up" clock as a for loop! */
for (var i = 1 ; i <= 5; i++) {
console.log(i);
}
/* Rewrite the for loop to have no initial conditions. */
var i = 1;
for (; i <= 5; i++) {
console.log(i);
}
/* Rewrite the for loop to have no incrementing statement. */
for (var i = 0; i < 5; ) {
console.log(++i);
}
/* Rewrite the for loop to have no incrementing or intial
statements. */
var i = 0;
for (; i < 5; ) {
console.log(++i);
}
/* Note that this is identical to the while loop! */
/* Let's make a count DOWN clock. We will display
5, 4, 3, 2, 1, 0. */
var i = 5;
while (i >= 0) {
console.log(i--);
}
/* Now, rewrite the countdown clock as a for loop! */
for (i = 5; i >= 0; i--) {
console.log(i);
}
/* Using a for loop, print each value of the array
to the console. */
var foods = ["Burger", "Fries", "Drink", "Tomato"];
for (i=0; i < foods.length; i++) {
console.log(foods[i]);
}
/* Using a for loop, print each value of the array
to the console, backwards! */
var foods = ["Burger", "Fries", "Drink", "Tomato"];
for (i=foods.length-1; i >= 0 ; i--) {
console.log(foods[i]);
}
/************************
* FUNCTIONS
************************/
/* Given the following function, call the function with
appropriate example values. In a comment, write
what the expected output will be. */
function concatStrings(str, appendString) {
return str + appendString;
}
concatStrings(null); // output is: NaN
/* Write a function that takes one number as a parameter,
and returns that number plus one. Choose good
variable names! Call the function with an appropriate
parameter. */
function increment(numberToIncrement) {
return numberToIncrement + 1;
}
increment(5); // output is: 6
/* Complete the below function. It accepts a
number and returns a string with "a" repeated
that many times. For example, calling the
function with 3 will return "aaa".
Use a for loop to construct the return
string. Call the function
with example values and write the output as
a comment. */
function repeatA(numTimesRepeated) {
var returnString = "";
for (var i = 0; i < numTimesRepeated; i++) {
returnString += "a";
}
return returnString;
}
repeatA(5); // output is: aaaaa
/* Let's generalize the last function. Now, a
second parameter will be passed which is the
character (or string) to repeat! Call the function
with example values and write the output as
a comment. */
function repeatString(numTimesRepeated, stringToRepeat) {
var returnString = "";
for (var i = 0; i < numTimesRepeated; i++) {
returnString += stringToRepeat;
}
return returnString;
}
repeatString(5, "d"); // output is: ddddd
/* Write a function which returns the original array,
but reversed! */
function reverseStrings(arrayOfStrings)
{
var reversedArray = [];
for (i=arrayOfStrings.length-1; i >= 0; i--) {
reversedArray.push(arrayOfStrings[i]);
}
return reversedArray;
}
var foods = ["Burger", "Fries", "Drink", "Tomato"];
reverseStrings(foods);
/* Note that variables can be assigned functions. */
var someVariable = repeatA;
var anotherVariable = repeatA;
/* Try running the above two lines in your browser.
Now, try calling the functions by using the
new variables. (i.e. run the below code!) */
someVariable(3);
anotherVariable(5);
/* This is an important fact about JavaScript --
functions can be assigned to variables and
passed around as arguments to functions! */
/* CONGRATULATIONS! YOU MADE IT TO THE END! */