-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlab.js
More file actions
201 lines (133 loc) · 4.98 KB
/
lab.js
File metadata and controls
201 lines (133 loc) · 4.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
let fullName = "Alex Trunzo";
fullName = fullName.toUpperCase();
lastName = fullName.slice(5, 11);
console.log(fullName);
firstName = fullName.slice(0, 4);
console.log(firstName);
console.log(firstName.includes(lastName));
function capFirstName(fullName){
return "Hello " + fullName.toUpperCase;
}
//1. What does `.length` do?
// `.length` returns the number of characters in a string.
//2. What does `.includes()` return?
// `.includes()` returns a boolean indicating whether a substring is found within a string.
//3. What does `.slice()` do?
// `.slice()` extracts a portion of a string and returns it as a new string.
let num1=10;
let num2 = 3;
let sum = num1 + num2;
let difference = num1 - num2;
let product = num1 * num2;
let quotient = num1 / num2;
console.log("Sum: " + sum);
console.log("Difference: " + difference);
console.log("Product: " + product);
console.log("Quotient: " + quotient);
let remainder = num1 % num2;
console.log("Remainder: " + remainder);
let roundedNum = Math.round(3.7);
console.log("Rounded Number: " + roundedNum);
let random = Math.floor(Math.random() * 10) + 1;
console.log("Random Number: " + random);
function evenOrOdd(num){
if(num % 2 === 0){
return "Even";
} else {
return "Odd";
}
}
console.log(evenOrOdd(10)); // Output: Even
console.log(evenOrOdd(7)); // Output: Odd
//1. What does `%` do?
// `%` returns the remainder of a division operation.
//2. What does `Math.random()` return?
// `Math.random()` returns a random decimal number between 0 (inclusive) and 1 (exclusive).
//3. When would you use `Math.floor()`?
// `Math.floor()` is used to round a decimal number down to the nearest integer.
let students = ["A", "B", "C", "D"];
for (let student in students){
console.log(students[student]);
}
students.push("E");
console.log(students);
students.pop();
console.log(students);
console.log(students.length);
function sumNums(nums){
let sum = 0;
for (let i = 0; i < nums.length; i++){
sum += nums[i];
}
return sum;
}
console.log(sumNums([1, 2, 3, 4, 5])); // Output: 15
//1. How do you access an array element?
// You can access an array element using its index, for example: `array[index]` where `index` starts at 0 for the first element.
//2. What does `.push()` do?
// `.push()` adds one or more elements to the end of an array.
//3. Why do we use loops with arrays?
// We use loops with arrays to iterate through all the elements and perform operations on them.
let car = {
brand: "Toyota",
model: "Camry",
year: 2020
};
console.log(car.brand);
console.log(car.model);
console.log(car.year);
car.year = 2021;
console.log(car);
car.color="red";
console.log(car);
for (let key in car){
console.log(key + ": " + car[key]);
}
function tellAge(person){
return person.name + " is " + person.age + " years old.";
}
//1. What is a key-value pair?
// A key-value pair is a way to store data in an object, where the key is a string and the value can be any data type.
//2. How do you access object data?
// You can access object data using dot notation (e.g., `object.key`) or bracket notation (e.g., `object["key"]`).
//3. When would you use an object instead of an array?
// You would use an object instead of an array when you want to store data with named properties rather than indexed elements.
let student = {
name: "Alex",
age: 23,
scores: [100, 100, 0]
}
function averageScore(student){
let sum = 0;
for (let score in student.scores){
sum += student.scores[score];
}
return sum / student.scores.length;
}
function grader(student){
let grade;
let average = averageScore(student);
if (average >= 90){
grade = "A";
} else if (average >= 80){
"B";
} else if (average >= 70){
grade = "C";
} else if (average >= 60){
grade = "D";
} else {
grade = "F";
}
console.log("Name: " + student.name);
console.log("Average Score: " + average);
console.log("Grade: " + grade);
}
grader(student);
//1. Which data type felt easiest?
//The numbers felt easiest to work with because they have straightforward operations and functions in JavaScript.
//2. Which one was most confusing?
//The objects were the most confusing because they have a more complex structure and require understanding of key-value pairs and how to access data.
//3. How do arrays and objects differ?
//Arrays are ordered collections of elements that can be accessed by their index, while objects are collections of key-value pairs where the keys are strings and the values can be any data type. Arrays are typically used for lists of items, while objects are used for more complex data structures with named properties.
//4. When would you use each in real applications?
//You would use arrays when you need to store and manipulate a list of items, such as a list of users or products. You would use objects when you need to represent more complex data with multiple properties, such as a user profile with name, age, and email.