forked from devleague/js-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
252 lines (213 loc) · 4 KB
/
functions.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
/* @@ -3,21 +3,27 @@
* @param {number} n
* @return {string} the number as a string
*/
function numberToString(number){
return number.toString();
}
/**
* Adds one to a given number.
* @param {number} n
* @return {number}
*/
function increase(number){
return number+1;
}
/**
* Subtracts one from a given number.
* @param {number} n
* @return {number}
*/
function decrease(number){
return number-1;
}
/**
* Adds two numbers.
@@ -25,7 +31,9 @@
* @param {number} y
* @return {number} the sum
*/
function add(x, y){
return x+y;
}
/**
* Subtracts the second number from the first.
@@ -33,7 +41,9 @@
* @param {number} y
* @return {number} the difference
*/
function subtract(x, y){
return x-y;
}
/**
* Multiplies two numbers.
@@ -41,7 +51,9 @@
* @param {number} y
* @return {number} the product
*/
function multiply(x, y){
return x*y;
}
/**
* Divides the first number by the second.
@@ -49,14 +61,18 @@
* @param {number} y
* @return {number} the quotient
*/
function divide(x, y){
return x/y;
}
/**
* Multiplies a number by itself.
* @param {number} x, number to be squared
* @return {number} squared
*/
function square(x){
return x*x;
}
/**
* Performs a mathematical operation on two numbers.
@@ -66,7 +82,29 @@
* @param {number} y
* @return {number} the result
*/
function calculate(operation, x, y){
var result, str;
switch(operation){
case "add":
result = x+y;
str = x+" + "+y+" = "+result;
break;
case "subtract":
result = x-y;
str = x+" - "+y+" = "+result;
break;
case "multiply":
result = x*y;
str = x+" * "+y+" = "+result;
break;
case "divide":
result = x/y;
str = x+" / "+y+ " = "+ result;
break;
}
console.log(str);
return result;
}
/**
* Returns true if `a` is greater than `b`.
@@ -74,7 +112,9 @@
* @param {number} b
* @return {boolean} `a` is larger than `b`
*/
function isGreaterThan(a, b){
return a>b;
}
/**
* Returns true if `a` is less than `b`.
@@ -82,7 +122,9 @@
* @param {number} b
* @return {boolean} `a` is smaller than `b`
*/
function isLessThan(a,b){
return a<b;
}
/**
* Returns true if `a` and `b` are equal.
@@ -90,7 +132,9 @@
* @param {number} b
* @return {boolean} the numbers are equal
*/
function areEqual(a,b){
return a===b;
}
/**
* Returns the smallest value of two numbers.
@@ -98,7 +142,9 @@
* @param {number} y
* @return {number} the smallest number
*/
function minimum(a,b){
return (a>b)?b:a;
}
/**
* Returns the largest value of two numbers.
@@ -106,21 +152,27 @@
* @param {number} y
* @return {number} the largest number
*/
function maximum(a,b){
return (a>b)?a:b;
}
/**
* Returns true if `n` is even.
* @param {number} n
* @return {boolean} the number is even
*/
function isEven(n){
return (n%2==0)?true:false;
}
/**
* Returns true if `n` is odd.
* @param {number} n
* @return {boolean} the number is odd
*/
function isOdd(n){
return ( n % 2 === 0 ) ? false : true;
}
/**
* Returns a letter grade.
@@ -133,6 +185,22 @@
* @param {number} total maximum possible score
* @return {string} the score represented as a letter grade
*/
function letterGrade(score, maxScore){
var perc = score*100/maxScore;
var str = "";
if(perc >= 90){
str = "A";
} else if (perc >= 80){
str = "B";
} else if (perc >= 70){
str = "C";
} else if (perc >= 60){
str = "D";
} else {
str = "F";
}
return str;
}
/**
@@ -142,7 +210,14 @@
* @param {object} restaurant represents a restaurant object
* @return {object} restaurant
*/
function incrementReviews(obj){
if(obj.reviews !== undefined){
obj.reviews++;
} else {
obj.reviews = 1;
}
return obj;
}
/**
* Joins two strings with a space.
@@ -150,7 +225,9 @@
* @param {string} word2
* @return {string} joined the words joined with a space
*/
function combine(a, b){
return a+" "+b;
}
/**
* Returns a circle object with the properties `circumference` and `area`.
@@ -159,4 +236,9 @@
* @param {number} radius
* @return {object} circle
*/
function createCircle(r){
return {
circumference: 2 * Math.PI * r,
area: r*r*Math.PI
}
}