-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlab.js
More file actions
387 lines (232 loc) · 5.98 KB
/
lab.js
File metadata and controls
387 lines (232 loc) · 5.98 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
# Part 1: Strings
## What is a String?
A string is text wrapped in quotes.
```javascript
let name = "Coreye";
``` {data-source-line="60"}
---
## 🔑 Common String Methods
```javascript
let message = "hello world";
message.length // length of string
message.toUpperCase() // HELLO WORLD
message.toLowerCase() // hello world
message.includes("world") // true
message.slice(0, 5) // hello
``` {data-source-line="74"}
---
## ✅ Practice 1: String Manipulation
Create a variable:
```javascript
let fullName = "your name here";
``` {data-source-line="84"}
### Tasks:
* Print the length of the string
* Convert it to uppercase
* Check if it includes your last name
* Slice out your first name only
---
*/
let fullName = "coreye ross";
console.log(fullName.length);
fullName = fullName.toUpperCase();
console.log(fullName);
let nameSplit = fullName.split(" ");
console.log("Has last name: " + nameSplit.length > 1);
console.log("First Name only: " + nameSplit[0]);
/*
1. What does `.length` do?
Returns length of the string.
2. What does `.includes()` return?
Checks if the substring is included, returns true or false.
3. What does `.slice()` do?
Slices string at starting index and ending index.
## Mini Challenge
Create a function that:
* Accepts a name
* Returns: `"Hello, NAME!"` (all caps)
*/
function sayHello(name) {
name = name.toUpperCase();
return "Hello, " + name;
}
console.log("Mini Challenge: String, " + sayHello("Jayden"));
/*
---
## Practice 2: Number Logic
Create variables:
```javascript
let num1 = 10;
let num2 = 3;
```
### Tasks:
* Add, subtract, multiply, divide
* Find remainder
* Round a decimal number
* Generate a random number between 1–10
---
*/
let num1 = 10;
let num2 = 3;
num1++;
console.log("Num1 + 1: " +num1);
num1--;
console.log("Num1 - 1: " + num1)
num2 *= num1;
console.log("Num2 * Num1: =" + num2);
num1 /= num2;
console.log("Num1 / Num2: " + num1);
console.log("Final Result Num1: " + num1);
console.log("Final Result Num2: " + num2);
console.log("Final Result Num1 (rounded to nearest integer): " + Math.round(num1));
let randomNum = Math.floor((Math.random() * 10) + 1);
console.log("Random Number: " + randomNum);
function checkOddEven(number) {
return number % 2 == 0 ? "Even" : "Odd";
}
console.log("Mini Challenge: Math, " + checkOddEven(22));
/*
## Check for Understanding
1. What does `%` do?
Modulus is used to find the remainder after a division operation between two numbers.
2. What does `Math.random()` return?
A random number between 0 (inclusive) and 1 (exclusive)
3. When would you use `Math.floor()`?
I would use Math.floor to round to the nearest whole number, such as when using Math.random
---
*/
/*
---
## Practice 3: Array Work
Create an array:
```javascript
let students = ["A", "B", "C", "D"];
``` {data-source-line="236"}
### Tasks:
* Print all students
* Add a new student
* Remove the last student
* Print total number of students
---
*/
//1.
let students = ["A", "B", "C", "D"];
for(let i = 0; i < students.length; i++) {
console.log(students[i]);
}
//2.
students.push("E");
//3.
students.pop();
//4.
console.log(students.length);
function calculateSum(nums) {
let sum = 0;
for(let i = 0; i < nums.length; i++) {
sum += nums[i];
}
return sum;
}
console.log("Mini Challenge: Arrays, " + calculateSum([1,2,3,4]));
/*
1. How do you access an array element?
//You access an array element via
// 0-based indexing.
// (from 0 to arr.length - 1)
2. What does `.push()` do?
push adds a new value to the end of the array
3. Why do we use loops with arrays?
we use loops in order to index each value of the array.
It's used to prevent writing repeat code from 0 to arr.length - 1
*/
/*
### Tasks:
* Print each property
* Update the year
* Add a new property (color)
* Loop through the object
---
*/
let car = {
brand: "Toyota",
model: "Camry",
year: 2020
};
//1.
for (let property in car) {
console.log(property);
}
//2.
car.year = 2026;
//3.
console.log("Looping through object");
for (let property in car) {
console.log(property, car[property]);
}
let person = {
name: "Bruh",
age: 25
}
function findAge(person) {
return person.name + " is " + person.age + " years old";
}
console.log("Mini Challenge: Objects, " + findAge(person));
/*
---
## Check for Understanding
1. What is a key-value pair?
A type of datastructure that's value can only be accesed through a key.
Grants O(1) access.
2. How do you access object data?
By providing the specific property field with the dot operator.
3. When would you use an object instead of an array?
You would use an object if you want to store data in fields, rather than a simple collection of items.
(The difference between a bird object and a list of bird objects for example)
---
*/
function calculateAvg(student) {
let sum = 0;
for(let i = 0; i < student.scores.length; i++) {
sum += student.scores[i];
}
const avg = sum / student.scores.length;
return avg;
}
let student = {
name: "Bob Builder",
scores: [80, 90, 75, 100]
}
console.log("Student:");
console.log(student.name);
const avg = calculateAvg(student);
if(avg >= 90) {
console.log("A");
}
else if(avg >= 80) {
console.log("B");
}
else if(avg >= 70) {
console.log("C");
}
else if(avg >= 60) {
console.log("D");
}
else {
console.log("F");
}
/*
---
# Reflection (REQUIRED)
At the bottom of your file, answer:
1. Which data type felt easiest?
Arrays felt the easiest as I'm the most familiar with them.
2. Which one was most confusing?
Objects were the most confusing as I've rarely looped thorugh all of the objects properties.
3. How do arrays and objects differ?
Arrays are based on a collection of a specific data type, an object is more like the description of a data type.
4. When would you use each in real applications?
I would make a bird object with wings and a bear, and two legs to represent a bird.
Then If I needed a lot of birds, there would be an array of bird objects.
---
*/