-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
376 lines (306 loc) · 12.3 KB
/
Copy pathindex.js
File metadata and controls
376 lines (306 loc) · 12.3 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/************************************************************** Task 1: Warm-up! **************************************************************/
//Task a: declare a variable called votingAge, console log true if age > 18 (no function required)
/* use let not var as var allows you to re-declare */
// declare variable
let votingAge;
// check age, print true if greater than 18
if(age > 18) {
// print true
console.log(true);
}
//Task b: declare a variable and then use a conditional to change the value of that variable based on the value assigned to a second variable (no function required)
// declare and initialize variables
let num1 = 1, num2 = 3;
if(num2 < 10) {
// Under this condition, we will increment num1.
num1++;
} else {
// otherwise, we will decrement num1.
num1--;
}
//Task c: Convert string ("1999") to integer (1999) (no function required) // hint look up the Number method
// decalre a string variable
let year = "1999";
// parseInt will take a valid set of characters and change them to integer values rather than a string
parseInt(year);
//parseInt will return undefined if String not made out of digits
//Task d: Write a function to multiply a*b
// declare two variables with values
let a = 13, b = 7;
// function definition with thw variables as parameters
function multiply(a, b) {
// return the value to where the function is called
return a * b;
}
/************************************************************** Task 2 **************************************************************/
//Age in Dog years
//write a function that takes your age and returns it to you in dog years - they say that 1 human year is equal to seven dog years
// Declare and initialize a human age
let age = 18;
// define function with age as parameter
function calcDogYears(age) {
// return the value for dog years
return 7*age;
}
/************************************************************** Task 3 **************************************************************/
//Dog feeder
//takes weight in pounds and age in years (note if the dog is a puppy the age will be a decimal) and returns the number of pounds of raw food to feed in a day.
//feeding requirements:
// adult dogs at least 1 year
// 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
// when you are finished invoke your function with the weight of 15 lbs and the age of 1 year - if your calculations are correct your result should be 0.44999999999999996
// dog weight and age
let weight = 15, age = 1;
// definition of function for feeding
function calcFeedDaily(weight, age) {
// at the least 1 year
if(age >= 1) {
// First condition weight is up to 5 lb
if(weight <= 5) {
return 0.05*weight;
}
// Second condition weight is above 6 and below 10 lb
else if(weight >= 6 && weight <= 10) {
return 0.04*weight;
}
// weight between 11 and 15 lb
else if(weight >= 11 && weight <= 15) {
return 0.03*weight;
}
// weight is over 15 lb
else if (weight > 15) {
return 0.02*weight;
}
// weight is not equal to or under or over 15 lb
else {
return null;
}
}
// Less than one year
else if (age < 1) {
// use number of months to determine percent of body weight to feed puppy
if(age*12 < 2) {
return null;
}
else if(age*12 >= 2 && age*12 <= 4) {
return 0.10*weight;
}
else if(age*12 > 4 && age*12 <= 7) {
return 0.05*weight;
}
else if(age*12 > 7 && age*12 <= 12) {
return 0.04*weight;
}
else {
return null;
}
}
}
/************************************************************** Task 4 **************************************************************/
// Rock, Paper, Sissors
// Your function should take a string (either rock, paper, or scissors)
// it should return you won or you lost based on the rules of the game (you may need to look up the rules if you have not played before)
// use math.random to determine the computers choice
// hint while you can complete this with only conditionals based on strings it may help to equate choice to a number
let yourChoice = "rock";
function findWinner(yourChoice) {
// Math.random gives a random number between 0 and 1
// Multiply by 10 to get a number between 0.23 or 1.5674 or 2.286 ... and 9.00017
// Truncate and only keep the 0, 1, 2, 3, ..., or 9
// Apply mod 3 to get remainders 0, 1, 2
// Add 1 to get results of only 1, 2, 3 for R, P, S
// So this method will produce a range of outputs, specifically 1, 2, 3
let compChoice = Math.trunc(Math.random*10)) % 3 + 1;
// Set the number to a string option for the computer choice
if (compChoice == 1) {
compChoice = "rock";
}
else if (compChoice == 2) {
compChoice = "paper";
}
else if (compChoice == 3) {
compChoice = "scissors";
}
// All options for user choice being rock, paper, or scissors.
if (yourChoice == "rock" && compChoice == "rock") {
return null;
}
else if (yourChoice == "rock" && compChoice == "scissors") {
return "you won";
}
else if (yourChoice == "rock" && compChoice == "paper") {
return "you lost";
}
else if (yourChoice == "scissors" && compChoice == "rock") {
return "you lost";
}
else if (yourChoice == "scissors" && compChoice == "paper") {
return "you won";
}
else if (yourChoice == "scissors" && compChoice == "scissors") {
return null;
}
else if (yourChoice == "paper" && compChoice == "rock") {
return "you won";
}
else if (yourChoice == "paper" && compChoice == "paper") {
return null;
}
else if (yourChoice == "paper" && compChoice == "scissors") {
return "you lost";
}
}
/************************************************************** Task 5 **************************************************************/
//Metric Converter
//a. KM to Miles - should take the number of kilometers and convert it to the equal number of miles
// mental plan:
// 0.621371 miles in 1 km
// km * mi / km ==> mi (km cancels)
// x km * 0.621371 mi / 1 km = 0.621371*x mi
function calcMiles(numKm) {
return 0.621371*numKm;
}
//let numKm = 5;
//console.log(calcMiles(numKm));
//b. Feet to CM - should take the number of feet and convert it to the equal number of centimeters
// mental plan:
// x ft * (12 in. / 1 ft) *( 2.54 cm / 1 in.)
// x*12*2.54
function calcCm(numFt) {
return numFt*12*2.54;
}
//let numFt = 5;
//console.log(calcCm(numFt));
/************************************************************** Task 6 **************************************************************/
// 99 bottles of soda on the wall
// create a function called annoyingSong
// the function should take a starting number as an argument and count down - at each iteration it should log (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(numStart) {
for(let i = numStart; i > 0; i--) {
console.log(i +" bottles of soda on the wall, "+ i +" bottles of soda, take one down, pass it around ");
// to print the new value for number of bottles of soda, decrement i
i--;
// then print the remainder of the song
console.log(i +" bottles of soda on the wall");
// then increment i and let the for loop iteration decrement it for the next line of the song
i++;
}
}
// let numStart = 99;
// annoyingSong(numStart);
/************************************************************** Task 7 **************************************************************/
//Grade Calculator
//write a javaScript program that takes a mark out of 100 and returns a corresponding letter grade
//90s should be A
//80s should be B
//70s should be C
//60s should be D
//and anything below 60 should be F
function calcGrade(mark) {
if(mark >= 90){
return "A";
} else if(mark >= 80 && mark < 90) {
return "B";
} else if(mark >= 70 && mark < 80) {
return "C";
} else if(mark >= 60 && mark < 70) {
return "D";
} else if(mark >= 50 && mark < 60) {
return "F";
} else if (mark >= 0 && mark < 50) {
return "You can always message your TL on slack! Practice makes perfect!";
} else {
return "Not a valid mark.";
}
}
// let mark = 87;
// calcGrade(mark);
/************************************************************** Stretch **************************************************************/
//Create a function that counts the number of vowels within a string. It should handle both capitalized and uncapitalized vowels.
// Hint - you may need to study tomorrow's traning kit on arrays
// try looking up the .includes() method
// mental strategy:
// Use string as an array, and a loop to check each position
// if the character is either a or A, e or E, i or I, o or O, u or U then count it as a vowel
// parameter taken into findNumVowels function is the string defined prior to the function call on line 287 within the console log statement
function findNumVowels(msg) {
// counter for the number of vowels in string
let vowels = 0;
// for loop checks the 0th character through one less than the total number of characters in string
for(let i = 0; i < msg.length; i++) {
// use conditional to check for vowels in the string called msg
if(msg[i] == 'a' || msg[i] == 'A' || msg[i] == 'e' || msg[i] == 'E' || msg[i] == 'i' || msg[i] == 'I'
|| msg[i] == 'o' || msg[i] == 'O' || msg[i] == 'u' || msg[i] == 'U') {
// increment counter
vowels++;
}
}
// after for loop return value of vowels counter
return vowels;
}
// to run code must have defined string
//let msg = "Hello, portfolios are hard.";
// call function in console log statement to print the number being returned
//console.log(findNumVowels(msg));
/************************************************************** Stretch **************************************************************/
//Take Rock, Paper, Scissors further
//update your rock, paper, scissors code below to take a prompt from a user using the window object
/*<h2>Rock, Paper, Scissors</h2>
<button onclick="findWinner()">What will you choose?</button>
<p id="rps"></p>
use window prompt method */
function findWinner() {
// use the propmt method to have a user enter an option.
let yourChoice = prompt("Enter rock, paper, scissors: ");
// Math.random gives a random number between 0 and 1
// Multiply by 10 to get a number between 0.23 or 1.5674 or 2.286 ... and 9.00017
// Truncate and only keep the 0, 1, 2, 3, ..., or 9
// Apply mod 3 to get remainders 0, 1, 2
// Add 1 to get results of only 1, 2, 3 for R, P, S
// So this method will produce a range of outputs, specifically 1, 2, 3
let compChoice = Math.trunc(Math.random*10)) % 3 + 1;
if (compChoice == 1) {
compChoice = "rock";
}
else if (compChoice == 2) {
compChoice = "paper";
}
else if (compChoice == 3) {
compChoice = "scissors";
}
if (yourChoice == "rock" && compChoice == "rock") {
return null;
}
else if (yourChoice == "rock" && compChoice == "scissors") {
return "you won";
}
else if (yourChoice == "rock" && compChoice == "paper") {
return "you lost";
}
else if (yourChoice == "scissors" && compChoice == "rock") {
return "you lost";
}
else if (yourChoice == "scissors" && compChoice == "paper") {
return "you won";
}
else if (yourChoice == "scissors" && compChoice == "scissors") {
return null;
}
else if (yourChoice == "paper" && compChoice == "rock") {
return "you won";
}
else if (yourChoice == "paper" && compChoice == "paper") {
return null;
}
else if (yourChoice == "paper" && compChoice == "scissors") {
return "you lost";
}
}