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
183 lines (169 loc) · 5.88 KB
/
app.js
File metadata and controls
183 lines (169 loc) · 5.88 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
// console.log("Hello World!\n==========\n");
// Exercise 1 Section
console.log("==========\n\nEXERCISE 1: Print odd numbers");
let userNumber = prompt("Enter a number:");
printOdds(userNumber);
prompt("Press enter to continue");
function printOdds(count) {
if (count > 0) {
for (let i = 0; i <= count; i++) {
if (i % 2 != 0) {
console.log(i);
}
}
}
else if (count < 0) {
for (let i = 0; i >= count; i--) {
if (i % 2 != 0) {
console.log(i);
}
}
}
else {
console.log(`Error: cannot count to ${count}`);
}
}
// Exercise 2 Section
console.log("==========\n\nEXERCISE 2: Legal to drive?");
function checkAge(userName, age) {
let aboveSixteen = (userName == "" || userName == undefined) ? "Congrats, you can drive!" : `Congrats ${userName}, you can drive!`;
let belowSixteen = `${(userName == "" || userName == undefined) ? "Sorry" : `Sorry ${userName}`}, but you need to wait until you're 16.`;
if (age == "" || age == undefined) {
console.log("Sorry, I don't have enough information to tell if you are old enough to drive.")
}
else if (age >= 16) {
console.log(aboveSixteen);
}
else {
console.log(belowSixteen);
}
}
let userName = prompt("Please enter your name:");
let age = prompt("Please enter your age:");
checkAge(userName, age);
prompt("Press enter to continue");
// Exercise 3 Section
console.log("==========\n\nEXERCISE 3: Which quadrant?");
let whichQuadrant = function cartesian(x, y) {
let quadrant;
if (x == 0 || y == 0) {
quadrant = `on the ${(x == 0 && y == 0) ? "origin" : (x == 0) ? "y axis" : "x axis"}`;
}
else if (x > 0) {
quadrant = (y > 0) ? 1 : 4;
}
else {
quadrant = (y > 0) ? 2 : 3;
}
console.log(quadrant);
return quadrant;
}
// whichQuadrant(0, 0);
// whichQuadrant(0, 1);
// whichQuadrant(1, 0);
// whichQuadrant(1, 1);
// whichQuadrant(-1, 1);
// whichQuadrant(-1, -1);
// whichQuadrant(1, -1);
// whichQuadrant();
// whichQuadrant(1);
let userX = prompt("Enter a point on the x axis:");
let userY = prompt("Enter a point on the y axis:");
console.log("Quadrant of your graph point:");
whichQuadrant(userX, userY);
prompt("Press enter to continue");
// Exercise 4 Section
console.log("==========\n\nEXERCISE 4: What type of triangle?");
// typeOfTriangle();
// typeOfTriangle(1);
// typeOfTriangle(1, 2);
// typeOfTriangle(5, 4, 3);
// typeOfTriangle(3, 5, 3);
// typeOfTriangle(3, 5, 5);
// typeOfTriangle(3, 3, 3);
// typeOfTriangle(6, 3, 3);
// typeOfTriangle(3, 3, 7);
// typeOfTriangle(3, 0, 3);
// typeOfTriangle(-1, -1, -1);
let userSideA = prompt("Enter the length of one side of your triangle:");
let userSideB = prompt("Enter the length of another side of your triangle:");
let userSideC = prompt("Enter the length of the last side of your triangle:");
typeOfTriangle(userSideA, userSideB, userSideC);
prompt("Press enter to continue");
function typeOfTriangle(a, b, c) {
let sides = [a, b, c];
sides.sort(function(x, y) {
return x - y;
});
a = sides[0];
b = sides[1];
c = sides[2];
let type;
if (a + b <= c || c == undefined || isNaN(a) || isNaN(b) || isNaN(c)) {
type = "invalid";
}
else {
if (a == c) {
type = "equilateral";
}
else if (a == b || b == c) {
type = "isosceles";
}
else {
type = "scalene";
}
}
console.log(type);
return type;
}
// Exercise 5 Section
console.log("==========\n\nEXERCISE 5: Data Plan Status");
// dataPlanStatus(50, 5, 12.82);
// dataPlanStatus(50, 5, 5.25);
// dataPlanStatus(50, 30, 50);
// dataPlanStatus(50, 30, 60.44);
// dataPlanStatus(50, 30, 40.1);
// dataPlanStatus(50, 50, 50);
// dataPlanStatus(50, 5, 55.36);
// dataPlanStatus(5, 5, 30.27);
// dataPlanStatus(50);
let planLimit = prompt("Enter your monthly data limit in GB:");
let day = prompt("How many days have passed since your monthly plan renewed?");
let usage = prompt("How much data have you used so far in GB?");
dataPlanStatus(planLimit, day, usage);
function dataPlanStatus(planLimit, day, usage) {
if (day < 1 || day > 30) {
console.log("ERROR: invalid day");
return;
}
if (planLimit <= 0 || planLimit == undefined || usage == undefined || day == undefined) {
console.log("ERROR: invalid plan");
return;
}
let daysRemaining = 30 - day;
let dataRemaining = Math.round((planLimit - usage) * 100) / 100;
let avgDailyUse = Math.round(planLimit / 30 * 100) / 100;
let avgActualUse = Math.round(usage / day * 100) / 100;
let avgTargetUse = Math.round(dataRemaining / daysRemaining * 100) / 100;
console.log(`DAY ${day} OF 30 (${daysRemaining} days remaining)`);
console.log(`Average daily use: ${avgActualUse} GB/day`);
if (dataRemaining < 0) {
console.log(`You have EXCEEDED your ${planLimit}GB monthly limit by ${-dataRemaining} GB.`);
console.log(`If you will continue to use more than ${avgDailyUse} GB/day, consider upgrading your plan.`);
}
else if (day == 30) {
console.log("You have stayed within your plan limit this month.");
console.log(`Unused data: ${dataRemaining} GB`);
}
else {
if (avgActualUse > avgDailyUse) {
console.log(`You are EXCEEDING your expected daily average of ${avgDailyUse} GB/day.`);
console.log(`At this rate, you will exceed your 30-day limit by ${avgActualUse * 30 - planLimit} GB.`);
console.log(`To stay within your data plan, use no more than ${avgTargetUse} GB/day for the next ${daysRemaining} days.`);
}
else {
console.log("You are on track to stay within your data limit this month.");
console.log(`You can use up to ${avgTargetUse} GB/day for the next ${daysRemaining} days.`);
}
}
}