-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
283 lines (199 loc) · 6.66 KB
/
app.js
File metadata and controls
283 lines (199 loc) · 6.66 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Exercise 1: maxOfTwoNumbers()
In this exercise, create a function named maxOfTwoNumbers.
It should take two numbers as inputs and return the larger number.
If they're equal, return either one.
Exercise 1 has been completed for you:
*/
const maxOfTwoNumbers = (x, y) => {
if (x >= y) {
return x;
} else {
return y;
}
}
console.log('Exercise 1 Result:', maxOfTwoNumbers(3, 9));
/*
Exercise 2: isAdult()
Write a function named isAdult. It should take an age (number)
and return 'Adult' if the age is 18 or over and 'Minor' otherwise.
Example: isAdult(21) should return 'Adult'.
Complete the exercise in the space below:
*/
function isAdult(age) {
if (age >= 18) {
return 'Adult';
} else {
return 'Minor';
}
}
console.log('Exercise 2 Result:', isAdult(21));
/*
Exercise 3: isCharAVowel()
Write a function named isCharAVowel that takes a single character as
an argument. It should return true if the character is a vowel and
false otherwise. For the purposes of this exercise, the character
y should not be considered a vowel.
Example: isCharAVowel('a') should return true.
Complete the exercise in the space below:
*/
function isCharAVowel(character) {
character = character.toLowerCase();
if (character === 'a' || character === 'e' || character === 'i' || character === 'o' || character === 'u') {
return true;
} else {
return false;
}
}
console.log('Exercise 3 Result:', isCharAVowel("a"));
/*
Exercise 4: generateEmail()
Create a function named generateEmail. It should take two strings:
a name and a domain. It should return a simple email address.
Example: generateEmail('johnsmith', 'example.com')
should return 'johnsmith@example.com'.
Complete the exercise in the space below:
*/
function generateEmail(name, domain) {
return name + '@' + domain;
}
console.log(generateEmail('johnsmith', 'example.com'));
console.log('Exercise 4 Result:', generateEmail("johnsmith", "example.com"));
/*
Exercise 5: greetUser()
Define a function called greetUser. It should take a name and a
time of day (morning, afternoon, evening) and return a personalized
greeting.
Example: greetUser('Sam', 'morning') should return "Good morning, Sam!"
Complete the exercise in the space below:
*/
function greetUser(name, timeOfDay) {
return `Good ${timeOfDay}, ${name}!`;
}
console.log('Exercise 5 Result:', greetUser("Sam", "morning"));
/*
Exercise 6: maxOfThree()
Define a function, maxOfThree. It should accept three numbers
and return the largest among them.
Example: maxOfThree(17, 4, 9) should return 17.
Complete the exercise in the space below:
*/
function maxOfThree(a, b, c) {
return Math.max(a, b, c);
}
console.log('Exercise 6 Result:', maxOfThree(5, 10, 8));
/*
Exercise 7: calculateTip()
Create a function called calculateTip. It should take two arguments:
the bill amount and the tip percentage (as a whole number).
The function should return the amount of the tip.
Example: calculateTip(50, 20) should return 10.
Complete the exercise in the space below:
*/
function calculateTip(billAmount, tipPercentage) {
return billAmount * (tipPercentage / 100);
}
console.log('Exercise 7 Result:', calculateTip(50, 20));
/*
Exercise 8: convertTemperature()
Write a function named convertTemperature.
It takes two arguments: a temperature and a string representing the
scale ('C' for Celsius, 'F' for Fahrenheit).
Convert the temperature to the other scale.
Example: convertTemperature(32, 'C') should return 89.6 (Fahrenheit).
Example: convertTemperature(32, 'F') should return 0 (Celsius).
Complete the exercise in the space below:
*/
function convertTemperature(temp, scale) {
if (scale === 'C' || scale === 'c') {
// Celsius → Fahrenheit
return temp * 9/5 + 32;
} else if (scale === 'F' || scale === 'f') {
// Fahrenheit → Celsius
return (temp - 32) * 5/9;
} else {
return 'Invalid scale. Use "C" or "F".';
}
}
console.log('Exercise 8 Result:', convertTemperature(32, "C"));
/*
Exercise 9: basicCalculator()
Create a function named basicCalculator.
It should take three arguments: two numbers and a string representing
an operation ('add', 'subtract', 'multiply', 'divide').
Perform the provided operation on the two numbers.
In operations where the order of numbers is important,
treat the first parameter as the first operand and the
second parameter as the second operand.
Example: basicCalculator(10, 5, 'subtract') should return 5.
Complete the exercise in the space below:
*/
function basicCalculator(num1, num2, operation) {
if (operation === 'add') {
return num1 + num2;
} else if (operation === 'subtract') {
return num1 - num2;
} else if (operation === 'multiply') {
return num1 * num2;
} else if (operation === 'divide') {
if (num2 === 0) {
return 'Error: Cannot divide by zero';
}
return num1 / num2;
} else {
return 'Invalid operation';
}
}
console.log('Exercise 9 Result:', basicCalculator(10, 5, "subtract"));
/*
Exercise 10: calculateGrade()
Define a function called calculateGrade.
It should take a numerical score and return the corresponding letter
grade (A, B, C, D, F).
For example, 90 and above yields an 'A', 80-89 is a 'B',
and 70-79 is a 'C', 60-69 is a 'D' and anything lower than a 60 is an 'F'.
Example: calculateGrade(100) should return A.
Complete the exercise in the space below:
*/
function calculateGrade(score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else if (score >= 60) {
return 'D';
} else {
return 'F';
}
}
console.log('Exercise 10 Result:', calculateGrade(85));
/*
Exercise 11: createUsername()
Define a function called createUsername.
It should take a first name and a last name and return a username.
The username should be a combination of the following:
- The first three letters of the first name.
- The first three letters of the last name.
- The total character count of the first and last name combined.
Example: createUsername('Samantha', 'Green') should return 'SamGre13'.
Complete the exercise in the space below:
*/
function createUsername(firstName, lastName) {
const firstPart = firstName.slice(0, 3);
const lastPart = lastName.slice(0, 3);
const totalLength = firstName.length + lastName.length;
return firstPart + lastPart + totalLength;
}
console.log('Exercise 11 Result:', createUsername("Samantha", "Green"));
/*
Exercise 12: numArgs()
Challenge yourself with numArgs.
This function should return the count of arguments passed to it when called.
Complete the exercise in the space below:
*/
function numArgs(...args) {
return args.length;
}
console.log('Exercise 12 Result:', numArgs(1, 2, 3, 4));