-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
306 lines (252 loc) Β· 9.13 KB
/
index.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*ποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈ Task 1: Warm-up! ποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈποΈββοΈ*/
/*MAKE SURE TO RETURN ALL OF THE ANSWERS INSIDE OF A FUNCTION (tasks 1d through task 7), IF YOU DON'T, THE AUTOGRADER WILL NOT WORK*/
// task 1a, 1b, and 1c are not autograded
/*
When doing these tasks, we recommend using console.log to test the output of your code to make sure it works correctly.
This will usually entail console logging the answer directly or console logging the invocation (call) of the function so when it
returns a value, that value will be logged to the console. An example of this would be: console.log(theFunction(value1,value2))
*/
/*
Task 1a - Voting Age
Do the following:
1. Create a variable called votingAge and assign it a value
2. Console log true if age is 18 or higher
HINT: no function required
*/
const votingAge = 25;
if ( votingAge >= 18){
console.log(true);
} else{
console.log(false);
}
/*
Task 1b - Values
Do the following:
1. Declare two variables and assign them values
2. Use a conditional to change the value of the 1st variable based on the value assigned to the 2nd variable
3. Console log the new value of the 1st variable
HINT: no function required
*/
let space = 'Amazing';
let earth = 'Boring';
if (earth === 'Boring'){
space = 'Out of this world!';
}
console.log (space);
/*
Task 1c - Convert Strings to Numbers
Do the following:
1. Declare a string type variable with the value of "1999"
2. Convert the string value of "1999" to a integer value of 1999
3. Console log the result
HINT: look up the Number method
*/
const newYear = '1999';
console.log(Number(newYear));
/*
Task 1d - Multiply
Do the following:
1. Invoke the multiply function below and pass it two numbers
2. Receive the parameters: a and b
3. Multiply a and b and return the answer
*/
function multiply(a,b){
return a * b;
}
/*ππππππππππ Task 2 ππππππππππ*/
//Age in Dog years
/*
Do the following:
1. Invoke the dogYears function below and pass an age value to it
2. Use the received value to calculate the age in dog years (1 human year is equal to 7 dog years)
3. Return the newly calculated age
*/
function dogYears(age){
return age * 7;
}
/*ππππππππππ Task 3 ππππππππππ*/
//Dog feeder - Depending on their weight and age, we need to know how many pounds of food to feed our dog each day!
/*
Use the hungryDog function and feeding requirements below to do the following:
1. Invoke the hungryDog function below and pass it a weight value in pounds
2. Also, pass to hungryDog an age value in years (note: if the dog is a puppy, the age will be a decimal. For example: three months = 3/12 or .25)
3. Do the proper calculations and return the number of pounds of raw food to feed the dog/puppy in a day
REMEMBER: This program should handle correctly adult AND puppy ages and weights
Feeding Requirements:
Adult Dogs 1 year and older
up to 5 lbs - 5% of their body weight
6 - 10 lbs - 4% of their body weight
11 - 15 lbs - 3% of their body weight
> 15lbs - 2% of their body weight
Puppies less than 1 year
2 - 4 months 10% of their body weight
4 - 7 months 5% of their body weight
7 - 12 months 4% of their body weight
NOTE: If done correctly, a weight of 15 lbs and age of 1 year would return 0.44999999999999996
*/
function hungryDog(weight, age){
if(age >= 1 && weight <= 5){
return weight * 0.05;
}
else if(age >= 1 && weight >= 6 && weight <= 10){
return weight * 0.04;
}
else if(age >= 1 && weight >= 11 && weight <= 15){
return weight * 0.03;
}
else if(age >= 1 && weight > 15){
return weight * 0.02;
}
else if (age < 1 && age >=0.583){
return weight * 0.04;
}
else if(age < 0.583 && age >= 0.333){
return weight * 0.05;
}
else if(age < 0.333){
return weight * 0.1;
}
else{
return 'error try again';
}
}
/*ππππππππππ Task 4 ππππππππππ*/
// Rock, Paper, Scissors - Let's play against the computer!
/*
Create a global variable that randomly generates the computer's choice
Use Math.random to determine the computers choice (Math.random gives a random number between 0 and 1)
NOTE: Computer must be a global variable. It will be passed into the function as an argument when the function is invoked.
HINT: While you can complete this with only conditionals based on strings, it may help to equate choice to a number when using Math.random()
Use the game function below to do the following:
1. Receive 2 parameters the user's choice and the computer's choice
2. Return whether the user won, lost, or tied based on these rules of the game described below - the strings returned need to match the strings below exactly.
- win should return "you win!"
- lose should return "you lose!"
- tie should return "it's a tie"
RULES OF THE GAME: Scissors beats Paper | Paper beats Rock | Rock beats Scissors | Or there's a tie
HINT: Remember that the order in which we pass in our arguments matters when it comes to parameters
*/
let computer = Math.random();
if(computer <= 0.34){
computer = 'rock';
}else if(computer <= 0.67){
computer = 'paper';
}else if(computer > 0.67){
computer = 'scissors';
}
function game(user, computer){
if(user === computer){
return `it's a tie`
}
else if (user === 'rock' && computer ==='scissors'){
return `you win!`;
}
else if (user === 'paper' && computer === 'rock'){
return 'you win!';
}
else if (user === 'scissors' && computer === 'paper'){
return `you win!`;
}
else{
return `you lose!`;
}
}
/*ππππππππππ Task 5 ππππππππππ*/
//Metric Converter
//Task 5a - KM to Miles
/*
Using the miles function below do the following:
1. Receive a number of kilometers
2. Convert the number of kiolmeters received to miles
3. Return the number of miles
*/
function miles(km){
return km * 0.621371;
}
//Task 5b - Feet to CM
/*
Using the feet function below do the following:
1. Receive a number of cm
2. Convert the number of cm to feet
3. Return number of feet
*/
function feet(cm){
return cm / 30.48;
}
/*ππππππππππ Task 6 ππππππππππ*/
// Let's Sing 99 Bottles of Soda on the Wall!
/*
Using the annoyingSong function below do the following:
1. Receive a starting number and start the count down from the number received
2. At each invocation, it should RETURN this string (note: the tests are expecting the same string as below):
"{number} bottles of soda on the wall, {number} bottles of soda, take one down pass it around {number left over} bottles of soda on the wall"
*/
function annoyingSong(number){
for(let i = number; i > 0; i --){
return `${i} bottles of soda on the wall, ${i} bottles of soda, take one down pass it around ${i -1} bottles of soda on the wall`;
}
}
/*ππππππππππ Task 7 ππππππππππ*/
//Grade Calculator
/*
Using the grade function below do the following:
1. Receive a score out of 100
2. Return the corresponding letter grade following this grade scale:
90-100 should return 'you got an A'
80-89 should return 'you got a B'
70-79 should return 'you got a C'
60-69 should return 'you got a D'
below should return 'you got an F'
*/
function grade(number){
if (number >= 90){
return `you got an A`;
}
else if(number < 90 && number >= 80){
return`you got a B`;
}
else if (number < 80 && number >= 70){
return `you got a C`;
}
else if (number < 70 && number >= 60){
return`you got a D`;
}
else if (number < 60){
return `you got an F`;
}
}
/*πͺπͺπͺπͺπͺπͺπͺπͺπͺπͺ Stretch πͺπͺπͺπͺπͺπͺπͺπͺπͺπͺ*/
//Vowel Counter - How many vowels are there?
/*
Using the vowelCounter function below do the following:
1. Receive a string as a parameter
2. Count and return the number of vowels within that string. It should handle both capitalized and uncapitalized vowels.
HINT - you may need to study tomorrow's content on arrays
HINT - try looking up the .includes() method
*/
function vowelCounter(string){
let counter =0;
for (let i = 0; i < (string.length()); i ++){
if (string[i] === 'a' || string[i] === 'A' || string[i] === 'e' || string[i] === 'E' || string[i] === 'i' || string[i] === 'I' || string[i] === 'o' || stirng[i] === 'O' || string[i] === 'u' || string[i] === 'U');
counter ++;
}
return counter;
}
/*ππππππππππ Please do not modify anything below this line ππππππππππ*/
function foo(){
console.log('its working');
return 'bar';
}
foo();
/*ππππππππππ Don't touch the code after this line! ππππππππππ*/
module.exports = {
foo,
multiply,
dogYears,
hungryDog,
game,
miles,
feet,
annoyingSong,
grade
}