forked from benrbryant/JavaScript_Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
221 lines (176 loc) · 6.1 KB
/
app.js
File metadata and controls
221 lines (176 loc) · 6.1 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
console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("EXERCISE 1:\n==========\n");
// 1. Declare a function named `printOdds` using declaration notation (`function <name>(){...}`)
// 2. The function should take in one parameter named `count`
// 3. The function body should:
// - contain a `for loop` that counts to the parameter passed in,
// - an `if/else` statement that determines if the current value is odd,
// - and then `console.log()` the odd value to the console
// 4. BONUS: Now that you do not know exactly what number may be passed in as `count`, use an `if` statement to account for a negative `count` being passed in.
function printOdds(count)
{
if (count < 0)
{
count = Math.abs(count);
}
for (i = 1; i <= count; i++)
{
if (i % 2 != 0)
{
console.log(i);
}
}
}
printOdds(10);
// Exercise 2 Section
console.log("EXERCISE 2:\n==========\n");
// 1. Declare a function named `checkAge` using declaration notation (`function <name>(){...}`)
// 2. The function should take in two-parameters named userName & age
// 3. The function body should:
// - declare and initialize an aboveSixteen local variable with string value: "Congrats ${userName}, you can drive!",
// - declare and initialize an belowSixteen local variable with string value: "Sorry ${userName}, but you need to wait until you're 16.",
// - an `if/else` statement that determines if the `age` value is below 16,
// - and then `console.log()` the correct message to the console.
// 4. BONUS: Remember that paramaters are optional, and no `name` or `age` value could be passed in. Correctly account for no parameter being passed in.
function checkAge(userName, age)
{
if (userName === undefined || age === undefined)
{
console.log("Both userName and age must be provided.");
return;
}
let aboveSixteen = `Congrats ${userName}, you can drive!`;
let belowSixteen = `Sorry ${userName}, but you need to wait until you're 16`;
if (age < 16)
{
console.log(belowSixteen);
}
else
{
console.log(aboveSixteen)
}
}
checkAge(`Eric`, 25);
// Exercise 3 Section
console.log("EXERCISE 3:\n==========\n");
// Write a function that receives two parameters, x and y coordinates in
// the Cartesian plane, and prints out a message indicating if the point (x, y) lies on either the x or y axis,
// or what quadrant the point lies in.
// For example:
// - (0, 2) is on the x axis
// - (1, 2) is in Quadrant 1
// - (-6, 18) is in Quadrant 2
function whatQuadrant(x, y)
{
if (x == 0 && y != 0)
{
console.log(`${x},${y} is on the Y axis`);
}
else if (x != 0 && y == 0)
{
console.log(`${x},${y} is on the X axis`);
}
else if (x > 0 && y > 0)
{
console.log('QUadrant 1');
}
else if (x > 0 && y < 0)
{
console.log(`Quadrant 4`);
}
else if (x < 0 && y < 0)
{
console.log(`Quadrant 3`);
}
else if (x < 0 && y > 0)
{
console.log(`Quadrant 2`);
}
else if (x == 0 && y == 0)
{
console.log('No axis or quadrant')
}
}
whatQuadrant(1, 1);
whatQuadrant(-1, 1);
whatQuadrant(1, -1);
whatQuadrant(-1, -1);
// Exercise 4 Section
console.log("EXERCISE 4:\n==========\n");
// Write a function that receives three numbers that represent the lengths of a triangles three sides. If the three
// sides do not form a valid triangle (the sum of two sides should always be greater than the length of the third side),
// you should return a message indicating an invalid triangle. Otherwise, return a string indicating the type of triangle
// the three sides make (equilateral: all side lengths equal, isosceles: only two side lengths equal, or scalene: all different side lengths).
function isATriangle(a, b, c)
{
if ((a + b) > c || (a + c) > b || (b + c) > a)
{
if (a == b && a == c)
{
console.log(`Valid triangle, equilateral`);
}
if (a == b || a == c || b == c)
{
console.log(`Valid triangle, isosceles`);
}
if (a != b && a !=c && b != c)
{
console.log(`Valid triangle, scalene`);
}
}
else
{
console.log(`Not a valid triangle`);
}
}
isATriangle(25, 25, 25);
isATriangle(15, 30, 20);
isATriangle(22, 30, 15);
isATriangle(25, 25, 45);
// Exercise 5 Section
console.log("EXERCISE 5:\n==========\n");
// Write a function that will display feedback on cell phone data usage. Cell phone
// plans for this particular company give you a certain amount of data every 30 days
// which must be used or they are lost (no rollover). We want to track the average amount of data
// used per day and inform the user if they are using too much data or can
// afford to use more.
// Write a function that accepts the following parameters:
// - `planLimit`: amount of data in the plan per 30 day period
// - `day`: the current day in the 30 day period
// - `usage`: the total amount of data used so far
// The function should compute whether the user is over, under, or right on the average
// daily usage under the plan. It should also inform them of how much data is left
// and how much, on average, they can use per day for the rest of the month. If
// they’ve run out of data, it should inform them of that too.
function cellPlan(planLimit, day, usage)
{
if (usage >= planLimit)
{
if (usage > planLimit)
{
console.log(`You are over your limit.`);
}
else
{
console.log(`You have reached your limit.`);
}
}
else
{
let dataLeft = planLimit - usage;
let dataAverage = dataLeft / (30 - day);
if (dataAverage == day / usage)
{
console.log(`You are on track to continue using ${dataAverage} per day`)
}
else
{
console.log(`You need to use ${dataAverage} per day to be on track for the rest of the month`)
}
}
}
cellPlan(2200, 15, 1458);
cellPlan(2200, 10, 200);
cellPlan(2200, 29, 2200);
cellPlan(2200, 27, 2350);