forked from DevMountain/javascript-3-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnesting.js
203 lines (180 loc) · 5.19 KB
/
nesting.js
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
// =========================
// NESTING
// =========================
// === PROBLEM 1 ===========
var employees = [
{
firstName: "Von",
lastName: "Budibent",
email: "[email protected]",
department: "Sales"
},
{
firstName: "Catherina",
lastName: "Swalowe",
email: "[email protected]",
department: "Engineering"
},
{
firstName: "Theo",
lastName: "Trill",
email: "[email protected]",
department: "Services"
},
{
firstName: "Elsy",
lastName: "McCrorie",
email: "[email protected]",
department: "Legal"
},
{
firstName: "Lorie",
lastName: "Handsheart",
email: "[email protected]",
department: "Research and Development"
}
];
// Create a function called 'employeeUpdater'. employeeUpdater will loop over the array above and perform the following:
// 1. If employee's first name is Theo, remove that employee because he just got fired.
// 2. If the employee's first name is Lorie, change her department to 'HR'.
// 3. Return the updated employee array.
// Code here
// function employeeUpdater(employees) {
// employees = () =>
// employees.forEach(function(emp) {
// for (var key in emp) {
// if (emp["firstname"] === "Theo") {
// employees.splice(key);
// }
// if (emp["firstname"] === "Lorie") {
// emp.department(key) ='HR';
// }
// }
// });
// }
var employeeUpdater = () => {
for (var i = 0; i < employees.length; i++) {
if (employees[i].firstName === "Theo") {
employees.splice(i, 1);
}
if (employees[i].firstName === "Lorie") {
employees[i].department = "HR";
}
}
return employees;
};
// === PROBLEM 2 ==========
// The array below represents IDs tied to reported workplace accidents. An employee accidentally entered in duplicates to array, making it look as though there are more accidents this year than there actually are.
// 1. Write a function called 'removeDuplicates' that will remove all duplicate entries in the workplaceAccidents array.
// 2. Use nested for loops to check for duplicate numbers, and then remove the duplicates from the array.
// 3. Return the updated array.
var workplaceAccidents = [12, 56, 44, 3, 29, 56, 56, 3, 7, 12];
// Code here
function removeDuplicates() {
let newArr = [];
// workplaceAccidents.forEach((num, index) => {
// newArr.indexOf(num) === -1
// ? newArr.push(num)
// : workplaceAccidents.splice(index, 1);
// });
for (var i = 0; i < workplaceAccidents.length; i++) {
if (newArr.indexOf(workplaceAccidents[i]) === -1) {
newArr.push(workplaceAccidents[i]);
}
}
return newArr;
}
// === PROBLEM 3 ==========
var cat = {
name: "Fluffy",
catFriends: [
{
name: "Grumpy",
activities: ["be grumpy", "eat food"]
},
{
name: "Lazy Bones",
activities: ["sleep", "pre-sleep naps"]
}
]
};
// Fluffy has two friends, Grumpy and Lazy Bones.
// 1. Assign the value of Grumpy's 2nd activity to the variable below.
// 2. Assign fluffy2ndFriend the name of Fluffy's 2nd friend.
// Code here bj
var grumpyActivity = cat.catFriends[0].activities[1];
var fluffy2ndFriend = cat.catFriends[1].name;
// === PROBLEM 4 ==========
// Below is some information about my car. As you can see, I am not the best driver. I have caused a few accidents. Please update this driving record so that I can feel better about my driving skills.
// 1. Write a function called recordCleaner.
// 2. Loop over the accidents array.
// 3. Change atFaultForAccident from true to false.
var myCar = {
make: "Toyota",
model: "Corolla",
year: 1992,
accidents: [
{
date: "3/15/93",
damage: "$5,000",
atFaultForAccident: true
},
{
date: "7/4/98",
damage: "$2,200",
atFaultForAccident: true
},
{
date: "6/22/99",
damage: "$7,900",
atFaultForAccident: true
}
]
};
// Code here
// function recordCleaner(myCar) {
// for (let i = 0; i < myCar.accidents.length; i++) {
// if (myCar.accidents[i].atFaultForAccident) {
// myCar.accidents[i].atFaultForAccident = false;
// }
// }
// }
function recordCleaner() {
for (let i = 0; i < myCar.accidents.length; i++) {
if (myCar.accidents[i].atFaultForAccident) {
myCar.accidents[i].atFaultForAccident = false;
}
}
}
// === PROBLEM 5 ==========
// Below is an array of arrays. Use two for loops.
// 1. Write a function called 'looper'.
// 2. 'looper' should loop over the arrays.
// 3. If the number is odd, replace it with 'odd'.
// If the number is even, replace it with 'even'.
// 4. Return the modified numsArr.
var numsArr = [[1, 2, 3, 4], [5, 6], [7, 8, 9, 10, 11]];
// function looper(nums) {
// for (var i = 0; i < nums.length; i++) {
// for (var j = 0; j < nums[i].length; j++) {
// if (nums[j] % 2 === 0) {
// nums[j] = 'even';
// } else if {
// nums[j] = 'odd';
// }
// }
// }
// return nums;
// }
function looper() {
for (let i = 0; i < numsArr.length; i++) {
for (let j = 0; j < numsArr[i].length; j++) {
if (numsArr[i][j] % 2 === 0) {
numsArr[i][j] = "even";
} else {
numsArr[i][j] = "odd";
}
}
}
return numsArr;
}