-
Notifications
You must be signed in to change notification settings - Fork 0
/
w1d1.js
executable file
·219 lines (159 loc) · 4.38 KB
/
w1d1.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
// Introduction to Functions
// exercises from https://learn.makerpass.com/groups/reactor-prep-am-20161017/courses/reactorcore/reactorprep?id=lessons%2Fintro-functions
// Complete the function cube that returns the cube of x
function cube(x) {
return x * x * x;
}
cube(4);
// => 64
// Complete the function fullName that should take two parameters, firstName and lastName, and returns the firstName and lastName concatenated together with a space in between.
function fullName(firstName, lastName){
return firstName + ' ' + lastName;
}
fullName('Morgan', 'Simpson')
//=> 'Morgan Simpson'
// Write a function average that takes two numbers as input (parameters), and returns the average of those numbers.
function average(n1, n2){
return (n1 + n2) / 2;
}
average(10,30);
//=> 20
// Write a function greeter that takes a name as an argument and greets that name by returning something along the lines of "Hello, <name>!"
function greeter(name){
return 'Hello ' + name + '!';
}
greeter('Shawn');
//=> 'Hello Shawn!'
// Write a function greeter that takes a name as an argument and greets that name by returning something along the lines of "Hello, <name>!"
function greeter(name){
return "Hello, " + name + "!";
}
greeter("Shawn");
// Translate these geometric formulas (the area & perimeter of a square, rectangle, parallelogram, trapezoid, and triangle and area & circumference of a circle) intro JavaScript functions.
// Square
function areaSquare(s) {
//A = s^2
return Math.pow(s, 2);
}
areaSquare(4);
// =>16
function perimSquare(s) {
//P = 4s
return 4 * s;
}
perimSquare(4);
// =>16
// Rectangle
function areaRect(l, w) {
//A = lw
return l * w;
}
areaRect(8,2);
// => 16
function perimRect(l, w) {
//P = 2l + 2w
return 2 * (l + w);
}
perimRect(6,2);
// => 16
// Parallelogram
function areaPrllgrm(l, h) {
//A = lh
return l * h;
}
areaPrllgrm(8,2);
// => 16
function perimPrllgrm(l, w) {
//P = 2l + 2w
return (2 * l) + (2 * w);
}
perimPrllgrm(6,2);
// => 16
// Trapezoid
function areaTrap(h, b1, b2) {
//A =1/2h(b1 + b2)
return (h / 2) * (b1 + b2);
}
areaTrap(2,8,8);
// => 16
function perimTrap(s1, s2, b1, b2) {
//P = s1 + s2 + b1 + b2
return s1 + s2 + b1 + b2;
}
perimTrap(2, 5, 6, 3);
// => 16
// Triangle
function areaTri(b, h) {
//A = 1/2bh
return (b * h) / 2;
}
areaTri(8, 4);
// => 16
function perimTri(s1, s2, b) {
//P = s1 + s2 + b
return s1 + s2 + b;
}
perimTri(5, 7, 4);
// => 16
// Circle
function areaCir(r) {
//A = πr^2
return Math.PI * (r * r);
}
areaCir(8);
// => 201.06192982974676
function circCirc(r) {
//C = 2π * r
return (2 * Math.PI) * r;
}
circCirc(8);
// => 50.26548245743669
// some math methods used
// pi
Math.PI; // => 3.141592653589793
// square root
Math.sqrt(256); // => 16
//exponents
Math.pow(4, 3); // 4^3 or 4*4*4 => 64
// Write a function futureValue that can be used to calculate the future value of a quantity of money using compound interest.
// Use the function to calculate what the future value of $1700 (P = 1700) deposited in a bank that pays an annual interest rate of 4.7% (i = 0.047), compounded quarterly (n = 4) after 6 years (t = 6) (you can use Math.pow to do exponentiation).
function futureValue(P, i, n, t) {
return P*Math.pow((1 + i/n), n*t);
}
futureValue(1700, 0.047, 4, 6);
// => 2250.1218394891257
// Write a power function that accepts the parameters base and exponent and returns the result. Replace square and cube with the power function you just wrote. Do not use Math.pow.
function power(base, exponent){
if(exponent === 0){
return 1;
} else {
return base * power(base, --exponent); //exponent-1
}
}
power(4,3);
//=> 64
function power(base, exponent) {
var result = 1;
while(exponent--) {
result *= base;
}
return result;
}
power(4,3);
//=> 64
// Write your own square-root function called sqrt that accepts a number parameter and returns an approximate square root. Square-root approximations make use of averages. Be sure to use the average function you previously wrote. The first version of your square root function should perform no more than 3 successive averages.
function squareRoot(number, guess){
if (guess !== guess){
guess = number / 2;
}
var avgGuess = number / guess;
var newGuess = (avgGuess + guess) / 2;
if(guess === newGuess){
return guess;
}
return squareRoot(number, newGuess);
}
console.log(squareRoot(50,3));
//7.0710678118654755
console.log(squareRoot(256,48));
//16