-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab-answers.js
187 lines (142 loc) · 7.08 KB
/
lab-answers.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
/* Easy Going */
/* Get Even */
for (let i = 0; i <= 200; i++) {
if(i%2==0) {
console.log(i);
}
}
/* Fizz Buzz
Fizz Buzz
This is a classic problem that you should get really comfortable solving. If you've solved it before, try to make it more elegant and shorter.
*/
/*
Write a Javascript Application that logs all numbers from 1 - 100.
If a number is divisible by 3, log "Fizz" instead of the number.
If a number is divisible by 5, log "Buzz" instead of the number.
If a number is divisible by 3 and 5, log "FizzBuzz" instead of the number.
The commit message should read: "Fizz Buzz answered"
*/
for (let i = 0; i <= 100; i++)
if((i % 3 == 0) && !(i%5==0)){console.log("Fizz");}
else if ((i % 5 == 0) && !(i%3==0)){console.log("Buzz");}
else if ((i % 3 == 0)&&(i%5==0)){
console.log("Fizz Buzz");
} else {console.log(i);}
/*
Wild Wild Life
Use the following arrays to answer the questions below (name, species, age, hometown).
You should be modifying the elements by accessing them. It is up to you which methods to use.
Plantee just had her birthday; change Plantee's array to reflect her being a year older.
Change Wolfy's hometown from "Yukon Territory" to "Gotham City".
Give D'Art a second hometown by adding "Hawkins"
Porgee decides that Wolfy can't be named "Wolfy" anymore. Remove "Wolfy" from the wolfy array and replace it with "Gameboy".
The commit message should read: "Wild Wild Life answered"
*/
const wolfy = ["Wolfy", "wolf", 16, "Yukon Territory"]
const sharky = ["Sharky", "shark", 20, "Left Coast"]
const plantee = ["Plantee", "plant", 5000 , "Mordor"]
const porgee = ["Porgee", "Porg", 186, "Ahch-To"]
const dart = ["D'Art" , "Demogorgan Dog", 2, "Upside Down"]
plantee[2]+=1;
wolfy[3] = "Gotham City";
dart.push("Hawkins");
wolfy[0]="Gameboy";
/*
Yell at the Ninja Turtles
Create an array with the members of the ninja turtles (Donatello, Leonardo, Raphael, Michaelangelo).
Use a for of loop to call toUpperCase() on each of them and print out the result.
Haven't heard of for of loops?
As a developer, you'll be a lifelong learner and constantly encountering new things. We'll give you little stretches like this to get you used to looking at documentation and figuring some things out on your own. You've got this!
The commit message should read: "Yell at the Ninja Turtles answered"
Methods, Revisited
Here is a list of favorite movies:
const favMovies = ['Jaws', 'The Fellowship of the Ring', 'Howl\'s Moving Castle', 'Django Unchained', 'Cloud Atlas', 'The Usual Suspects', 'Toy Story', 'Conan the Barbarian', 'Titanic', 'Harry Potter', 'Fried Green Tomatoes', 'Volver', 'Oculus', 'Seven', 'Black Panther', 'Harry Potter', 'Imitation of Life', 'Snatch', 'Fast and Furious'];
Console log the index of Titanic.
Do the following and console.log the final results. (I have included some thought questions; you don't have to write out an answer for those marked as such).
Note: if you have to add to the array, feel free to add any movie you'd like.
Use the .sort method.
Thought Question: What did this do to the array? Did it permanently alter it?
Use the method pop.
push "Guardians of the Galaxy".
Reverse the array.
Use the shift method.
unshift - what does it return?
splice "Django Unchained" and add "Avatar" (try finding the index of "Django Unchained" instead of counting it yourself).
Thought Question: Did this permanently alter our array?
slice the last half of the array. (Challenge yourself and try to programatically determine the middle of the array rather than counting it and hard coding it.)
Thought Question: Did this permanently alter our array?
Store the value of your slice in a variable and console.log it.
Thought Question: What is going on here?
console.log your final results.
After running the above tasks, console.log the index of "Fast and Furious."
We removed it from the array. What value do we get when we look for the index of something that is not in the array?
Thought Question: We declared the variable favMovies with const, and yet we were allowed to change the array. Weird? Should we have used let?
The commit message should read: "Methods Revisited answered"
Where is Waldo
With the following multi-dimensional array:
const whereIsWaldo = [["Timmy", "Frank"], "Eggbert",
["Lucinda", "Jacc", "Neff", "Snoop"],
["Petunia", ["Baked Goods", "Waldo"]]];
Remove Eggbert (hint look at the slice/splice methods).
Change "Neff" to "No One".
Access and console.log "Waldo".
The commit message should read: "Where is Waldo answered"
Excited Kitten
Write code that logs "Love me, pet me! HSSSSSS!" 20 times.
For every even number in your loop, log "...human... why you taking pictures of me...?", "...the catnip made me do it...", or "...why does the red dot always get away...?" at random.
Need some hints? Click here!
Image of cat with funny quote about dot
The commit message should read: "Excited Kittens answered"
Find the Median
Find the median number in the following nums array, then console.log that number.
Hint: If you check the length of the array divided by two, you might get not get a whole number. In which case, look into Math.floor().
const nums = [14,11,16,15,13,16,15,17,19,11,12,14,19,11,15,17,11,18,12,17,12,71,18,15,12];
// Expected output: 15
The commit message should read: "Find the Median answered"
Hungry for More?
The remainder of these problems are optional; however, we encourage more practice whenever you have the time for it!
Below, we've given you examples of Kristyn and Thom's closets modeled as data in JavaScript. Use this data to answer the following questions.
const kristynsCloset = [
"left shoe",
"cowboy boots",
"right sock",
"Per Scholas hoodie",
"green pants",
"yellow knit hat",
"marshmallow peeps"
];
// Thom's closet is more complicated. Check out this nested data structure!!
const thomsCloset = [
[
// These are Thom's shirts
"grey button-up",
"dark grey button-up",
"light blue button-up",
"blue button-up",
],[
// These are Thom's pants
"grey jeans",
"jeans",
"PJs"
],[
// Thom's accessories
"wool mittens",
"wool scarf",
"raybans"
]
];
Alien Attire
Kristyn's left shoe has traveled through time and space and turned up in Thom's accessories drawer!
Remove Kristyn's shoe from the array and save it to the variable kristynsShoe.
Use that variable to add Kristyn's lost shoe to Thom's accessories array.
The commit message should read: "Kristyn's lost shoe found"
Dress Us Up
Modify your code to put together three separate outfits for Kristyn and Thom. Put the output in a sentence to tell us what we'll be wearing. Mix and match!
The commit message should read: "Kristyn and Thom have their outfits ready for class - array practice"
Dirty Laundry
Time to do laundry. Loop through Kristyn's closet and log the sentence "WHIRR: Now washing (item)" for each item in the array.
The commit message should read: "Washed dirty laundry"
Inventory
Thom wants to do inventory on his closet. Using bracket notation, log the arrays containing all of Thom's shirts, pants, and accessories.
The commit message should read: "Hungry for More: Complete"
*/