-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjavascript.js
221 lines (163 loc) · 5.91 KB
/
javascript.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/******************************************************************************
------------------------------------
!READ THE ASSIGNMENT TEXT CAREFULLY!
------------------------------------
1.
Use a normal 'for loop' to loop over the people array and do the following:
- If the objects 'name' value is "Otto", do not do any of the changes below to
that object (hint: the 'continue' keyword)
- Make a new key on each person object in the array called "city" and set the
value to a random city from the cities array.
- Make a new key on each person object called "title" and set it to "Mr." for
males and "Ms." for females.
- Increment the age by 2
- Add "coding" to the beginning of the hobby array on each object.
PS: Use only 1 loop to do the above steps.
console.log(people) after your loop to check that your changes are correct.
Use your loop to also calculate the combined
age of all the people objects and store it in the combinedAge variable.
Then, after the loop, use the combined age to calculate the average age
of everyone and store it in the averageAge variable.
Do your calculations AFTER you add the two years to the ages, and remember,
skip Otto!
******************************************************************************/
const cities = ["New York", "London", "Paris", "Berlin", "Copenhagen", "Rome"];
const people = [
{
name: "Thomas",
male: true,
age: 23,
hobbies: ["cycling", "football", "pool"]
},
{
name: "Susan",
male: false,
age: 26,
hobbies: ["jogging", "travelling", "dancing"]
},
{
name: "Monica",
male: false,
age: 21,
hobbies: ["skateboarding", "guitar", "concerts"]
},
{
name: "Avery",
male: true,
age: 28,
hobbies: ["writing", "games", "memes"]
},
{
name: "Phillip",
male: true,
age: 24,
hobbies: ["boxing", "wrestling", "mma"]
},
{
name: "Otto",
male: true,
age: 36,
hobbies: ["movies", "cinema", "music"]
},
{
name: "Annabelle",
male: false,
age: 30,
hobbies: ["makeup", "fashion", "shopping"]
},
{
name: "Cathy",
male: false,
age: 18,
hobbies: ["design", "drawing", "css"]
}
];
let combinedAge = 0;
//your code here
let averageAge = 0;
/******************************************************************************
2.
Make the following function:
It should take in 1 number as a parameter.
The function should return an array of random numbers between 1 and 6, the
length of the array is decided by the number the function receives as a
parameter (think of it as the number of dice we roll)
Examples:
diceRoller(4) should return something like: [4, 1, 2, 6]
diceRoller(6) should return something like: [5, 5, 6, 2, 3, 4]
EXTRA CREDIT:
(This is not mandatory)
Add a second parameter to the function that decides how many faces the dice
should have.
diceRoller(5, 20) should return an array of 5 random numbers ranging from 1-20
******************************************************************************/
/******************************************************************************
3.
Make the following function:
The function should take in 1 array of strings.
Inside the function, do this:
Write a loop that loops strings in the array, and does the following:
- Removes whitespace from the beginning and end of each word
- Converts all the words to lower case
Use a "FOR of" loop.
After the loop, use a method to join the array into a string with a single
space between the words (" "), and return the resulting string.
Example:
[" thIS", "teXt ", " nEeds ", "to", "BE", "cleANED ", " Up"]
should return:
"this text needs to be cleaned up"
******************************************************************************/
/******************************************************************************
4.
EXTRA CREDIT:
(This assignment is not mandatory, only for those who want an extra challenge)
Make a function called 'helloChecker' that takes in 1 string
as a parameter.
Write code that checks all the words in the string if they match the word for
'hello' in any of these languages:
hello - english
ciao - italian
salut - french
hallo - german
hola - spanish
czesc - polish
If any of the words in the string matches any of these, the function should
return: "HELLO detected in (name of the language)."
If none of the words in the string match any of the words for hello in the
different languages, your function should return: "No HELLO detected."
PS: Make sure the function is case insensitive, both 'Hello' and 'hello'
should be detected.
I have provided some string variables to test your function with.
******************************************************************************/
const greetings = [
"Hello, how are you today?",
"Diciamo ciao prima di andare!",
"Salut, ça va bien?",
"Kannst du mich hören? Hallo!",
"Hva er regex?",
"Nos saludamos con un alegre hola.",
"Ona pomachała i powiedziała cześć z uśmiechem.",
"Good afternoon gentlemen!"
];
/******************************************************************************
5.
EXTRA CREDIT:
(This assignment is not mandatory, only for those who want an extra challenge)
Complete the function below to accomplish the following:
Return the string received in the first parameter with the
following alterations:
any letter in the string matching charA (the second parameter in the function)
will be replaced with charB (the third parameter) and VICE VERSA - meaning
letters matching charA will be replaced with charB, and letters matching charB
will be replaced with charA. See the expected console log comments below.
Examples:
doubleSwap("this is a string", "i", "s")
should return "thsi si a itrsng"
doubleSwap("m#ybe #nother #ppro#ch is necess#ry", "#", "a")
should return "maybe another approach is necessary"
doubleSwap("what is the point of this?", "o", "t")
should return "whao is ohe ptino tf ohis?"
******************************************************************************/
function doubleSwap(string, charA, charB) {
//your code here
}