forked from DevMountain/javascript-basic-assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment.js
More file actions
241 lines (179 loc) · 6.58 KB
/
assessment.js
File metadata and controls
241 lines (179 loc) · 6.58 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
// #1 Create a variable called hello and assign it the string 'goodbye'
const hello = 'goodbye';
// #2 Use the variable iLove to create a new variable called iLoveCode that is assigned the string "I love code"
var iLove = 'I love';
const iLoveCode = iLove + ' code';
// #3 Make an object called bob and give it the following properties
// bob has a height of 6ft (string)
// bob has an age of 24 (Number)
// bob has hair, that has style spikey, and color brown (object)
// bob is not presidentOfTheUnitedStates (boolean)
// bob likes apples, bananas, and cherries (array of strings)
const bob = {
height: '6ft',
age: 24,
hair: { style: 'spikey', color: 'brown' },
presidentOfTheUnitedStates: false,
likes: ['apples', 'bananas', 'cherries']
}
// #4 Change my shirt color to pink using dot notation
var myShirt = {
type: 'polo',
color: 'red'
};
myShirt.color = 'pink';
// Change my shirt type to spandex using square bracket notation
var myOtherShirt = {
type: 'polo',
color: 'red'
};
myOtherShirt.type = 'spandex';
// #5 Create an object that tracks a count of animals in a zoo. Call it 'zoo'
// The key should be the animal name(string) and the value should be how many there are.
// Our zoo has 8 monkeys, 4 giraffes and 2 elephants
const zoo = {
monkeys: 8,
giraffes: 4,
elephants: 2
}
// #6 Loop through this object and change all keys that start with the letter s to have a value of 's'
var snake = {
sliters: 'sideways',
eats: 'rodents',
says: 'ssss',
smells: 'heat',
runs: 'legless'
};
console.log(snake.sliters);
for (var prop in snake) {
console.log(prop);
if (prop.startsWith('s')) {
snake[prop] = 's';
}
}
snake
//#7 Create an array of strings that are the 7 primary colors in the rainbow - red, orange, yellow, green, blue, indigo, violet (lower-case). Call your array rainbowColors
const rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// #8 Using this array do the following
var heroes = ['superman', 'batman', 'flash'];
// add 'wonderwoman' to the end
heroes.push('wonderwoman');
// remove 'superman' and store him in a variable called firstHero
const firstHero = heroes.shift();
// add 'spongebob' to the start of the array
heroes.unshift('spongebob');
// remove 'flash' from the array and store him in a variable called secondHero
const secondHero = heroes.splice(2, 1);
// leave batman in the array but put a copy of him on a variable called thirdHero
const thirdHero = heroes.slice(1, 2)[0];
// #9 Write a function called addItem that takes in an array and an item, adds the item to
// the array, and returns the array with the added item.
const addItem = (array, item) => {
array.push(item);
return array;
}
// #10 Write a function called removeItem that takes in an array of strings, and a string.
// Removes all instances of that string from the array. And return the modified array.
// The order of the array should not be changed
testArr = ['hi', 'this', 'is this', 'the', 'this', 'array', 'this', 'test'];
testStr = 'this';
const removeItem = (arr, str) => {
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i].includes(str)) {
console.log(i);
arr[i] = arr[i].replace(str, '');
}
if (arr[i] == '') {
arr.splice(i, 1);
}
}
arr
return arr;
}
removeItem(testArr, testStr);
// #11 Write a function called doubleTheFun that takes 1 parameter. It should double numbers, and
// repeats strings. example 4->8, 2.5->5, 'Awesome'->'AwesomeAwesome'
const test1 = 42;
const test2 = 'forty-two';
const test3 = '2.5'
console.log(test3 + test3);
const doubleTheFun = numOrString => {
if (typeof numOrString === 'string') {
if (!numOrString.match(/[a-z]/gi)){
return +numOrString + +numOrString;
} else {
return numOrString + numOrString;
}
} else {
return numOrString + numOrString;
}
}
console.log(doubleTheFun(test1));
console.log(doubleTheFun(test2));
console.log(doubleTheFun(test3));
// #12 Write function getValueOfProperty that takes in an object, and the name of a property on the object
// return the value from the object that corresponds to the property
const getValueOfProperty = (obj, prop) => {
return obj[prop];
}
// #13 Write a function called makeChatMessage that takes in a message and author as parameters
// and returns an object with a message, author, and timestamp, that is
// the current time as a Date object
const makeChatMessage = (message, author) => {
return {
message,
author,
timestamp: new Date()
}
}
// #14 Create a function called coderTest that takes in an object that is a obj. It looks to see if the obj’s name is Jeremy and then changes the obj object to have a property called lovesCode with a value of 10. If their name is Brack set lovesCode to 0. otherwise set lovesCode to 5.
const person = { name: 'Jeremy' };
const coderTest = obj => {
if (obj.name === 'Jeremy') {
obj.lovesCodes = 10;
} else if (obj.name === 'Brack') {
obj.lovesCode = 0;
} else {
obj.lovesCode = 5;
}
return obj
}
console.log(coderTest(person));
// #15 Create a function called outside that takes in a temperature (number), a humidity(number), and a cloudiness(number), in that order. Using the following to return the correct values
/*
temperature over 80 and humidity over 40 - return "I'm all sweat"
temperature under 40 and cloudiness over 60 - return "I have icecicles"
temperature over 80 and humidity under 40 and cloudiness under 20 - return "I'm literally in the desert"
temperature over 80 or humidity over 50 or cloudiness over 50 - return "Hmm, probably not"
Otherwise - return "I love outside"
*/
const outside = (temp, hum, cloud) => {
return temp > 80 && hum > 40
? "I'm all sweat"
: temp < 40 && cloud > 60
? "I have icecicles"
: temp > 80 && hum < 40 && cloud < 20
? "I'm literally in the desert"
: temp > 80 || hum > 50 || cloud > 50
? "Hmm, probably not"
: "I love outside"
// if (temp > 80 && hum > 40) {
// return "I'm all sweat"
// } else if (temp < 40 && cloud > 60) {
// return "I have icecicles"
// } else if (temp > 80 && hum < 40 && cloud < 20) {
// return "I'm literally in the desert"
// } else if (temp > 80 || hum > 50 || cloud > 50) {
// return "Hmm, probably not"
// } else {
// return "I love outside"
// }
}
console.log(outside(84, 42, 0))
// #16 Create a function called callerBack that takes in a function (holla) and a string parameter(back) and invokes it(holla) with the argument string(back) + ' back'."
// example - If I call you with 'Give it' you should invoke holla with 'Give it back'
const holla = (str) => str + ' back';
const callerBack = (fn, str) => {
return fn(str);
}
console.log(callerBack(holla, 'holla'));