-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-functions.js
More file actions
394 lines (334 loc) · 8.37 KB
/
03-functions.js
File metadata and controls
394 lines (334 loc) · 8.37 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// CAP 3 - FUNCTIONS
// Defining a Function
// 1
const square = function(x) {
return x * x;
};
console.log(square(12));
// ! 144
// 2
const makeNoise = function() {
console.log("Pling!");
};
makeNoise();
// ! Pling!
const power = function(base, exponent) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
console.log(power(2, 10));
// ! 1024
// Bindings and Scopes
// 3
let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// ! 60
}
// y is not visible here
console.log(x + z);
// ! 40
// 4
const halve = function(n) {
return n / 2;
};
let n = 10;
console.log(halve(100));
// ! 50
console.log(n);
// ! 10
// Nested Scope
// 5
const hummus = function(factor) {
const ingredient = function(amount, unit, name) {
let ingredientAmount = amount * factor;
if (ingredientAmount > 1) {
unit += "s";
}
console.log(`${ingredientAmount} ${unit} ${name}`);
};
ingredient(1, "can", "chickpeas");
ingredient(0.25, "cup", "tahini");
ingredient(0.25, "cup", "lemon juice");
ingredient(1, "clove", "garlic");
ingredient(2, "tablespoon", "olive oil");
ingredient(0.5, "teaspoon", "cumin");
};
// Functions as Values
//6
let launchMissiles = function() {
missileSystem.launch("now");
};
if (safeMode) {
launchMissiles = function() {/* do nothing */};
}
// Declaration Notation
// 7
function square(x) {
return x * x;
}
// 8
console.log("The future says:", future());
function future() {
return "You'll never have flying cars";
}
// Arrow Functions
// 9
const power = (base, exponent) => {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};
// 10
const square1 = (x) => { return x * x; };
const square2 = x => x * x;
// 11
const horn = () => {
console.log("Toot");
};
// The Call Stack
// 12
function greet(who) {
console.log("Hello " + who);
}
greet("Harry");
console.log("Bye");
// 13
function chicken() {
return egg();
}
function egg() {
return chicken();
}
console.log(chicken() + " came first.");
// ! ??
// Optional Arguments
// 14
function square(x) { return x * x; }
console.log(square(4, true, "hedgehog"));
// ! 16
// 15
function minus(a, b) {
if (b === undefined) return -a;
else return a - b;
}
console.log(minus(10));
// ! -10
console.log(minus(10, 5));
// ! 5
// 16
function power(base, exponent = 2) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
}
console.log(power(4));
// ! 16
console.log(power(2, 6));
// ! 64
// 17
console.log("C", "O", 2);
// ! C O 2
// Closure
// 18
function wrapValue(n) {
let local = n;
return () => local;
}
let wrap1 = wrapValue(1);
let wrap2 = wrapValue(2);
console.log(wrap1());
// ! 1
console.log(wrap2());
// ! 2
// 19
function multiplier(factor) {
return number => number * factor;
}
let twice = multiplier(2);
console.log(twice(5));
// ! 10
// Recursion
// 20
function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
console.log(power(2, 3));
// ! 8
// 21
function findSolution(target) {
function find(current, history) {
if (current == target) {
return history;
} else if (current > target) {
return null;
} else {
return find(current + 5, `(${history} + 5)`) || find(current * 3, `(${history} * 3)`);
}
}
return find(1, "1");
}
console.log(findSolution(24));
// ! (((1 * 3) + 5) * 3)
// Growing Functions
// 22
function printFarmInventory(cows, chickens) {
let cowString = String(cows);
while (cowString.length < 3) {
cowString = "0" + cowString;
}
console.log(`${cowString} Cows`);
let chickenString = String(chickens);
while (chickenString.length < 3) {
chickenString = "0" + chickenString;
}
console.log(`${chickenString} Chickens`);
}
printFarmInventory(7, 11);
// 23
function printZeroPaddedWithLabel(number, label) {
let numberString = String(number);
while (numberString.length < 3) {
numberString = "0" + numberString;
}
console.log(`${numberString} ${label}`);
}
function printFarmInventory(cows, chickens, pigs) {
printZeroPaddedWithLabel(cows, "Cows");
printZeroPaddedWithLabel(chickens, "Chickens");
printZeroPaddedWithLabel(pigs, "Pigs");
}
printFarmInventory(7, 11, 3);
// 24
function zeroPad(number, width) {
let string = String(number);
while (string.length < width) {
string = "0" + string;
}
return string;
}
function printFarmInventory(cows, chickens, pigs) {
console.log(`${zeroPad(cows, 3)} Cows`);
console.log(`${zeroPad(chickens, 3)} Chickens`);
console.log(`${zeroPad(pigs, 3)} Pigs`);
}
printFarmInventory(7, 16, 3);
// Summary
// 25
// Define f to hold a function value
const f = function(a) {
console.log(a + 2);
};
// Declare g to be a function
function g(a, b) {
return a * b * 3.5;
}
// A less verbose function value
let h = a => a % 3;
// Exercises
//Minimum
/*Chapter 2 introduced the standard function Math.min, which returns its smallest
argument (see “Return Values” on page 27). We can build something
like that now. Write a function min that takes two arguments and returns
their minimum.*/
function min_var1(a,b){
console.log(Math.min(a,b));
}
console.log(min_var1(7,3));
console.log(min_var1(9,222));
/*__________________________________________________________________________*/
function min_var2(a,b){
if(a < b){
console.log(a);
}else{
console.log(b);
}
}
console.log(min_var2(7,3));
console.log(min_var2(9,222));
//Recursion
/*We’ve seen that % (the remainder operator) can be used to test whether
a number is even or odd by using % 2 to see whether it’s divisible by two.
Here’s another way to define whether a positive whole number is even
or odd:
• Zero is even.
• One is odd.
• For any other number N, its evenness is the same as N 2.
Define a recursive function isEven corresponding to this description.
The function should accept a single parameter (a positive, whole number)
and return a Boolean.
Test it on 50 and 75. See how it behaves on 1. Why? Can you think of a
way to fix this?*/
function isEven(a){
if(a == 0){
return console.log(a + " is even", true);
}
if(a == 1){
return console.log(a + " is odd", false);
}
if(a <0){
return "??";
}else {
return isEven(a-2);
}
}
console.log(isEven(-1));
console.log(isEven(50));
console.log(isEven(75));
// Bean Counting
/*You can get the Nth character, or letter, from a string by writing "string"[N].
The returned value will be a string containing only one character (for
example, "b"). The first character has position 0, which causes the last one
to be found at position string.length - 1. In other words, a two-character
string has length 2, and its characters have positions 0 and 1.
Write a function countBs that takes a string as its only argument and
returns a number that indicates how many uppercase “B” characters there
are in the string.
Next, write a function called countChar that behaves like countBs, except
it takes a second argument that indicates the character that is to be counted
(rather than counting only uppercase “B” characters). Rewrite countBs to
make use of this new function. */
function countBs(prop){
let j = 0;
for(let i = 0; i <= prop.length - 1; i++)
{
if(prop[i]=='B')
{
j++;
}
}
return console.log(prop + " : counting the character B is equal with : " + j);
}
let prop = "ThisB is a B proposition B meant to BeB";
console.log(countBs(prop));
/*__________________________________________________________________________*/
function countChars(syntax, char){
let count = 0;
for(let i = 0; i <= syntax.length - 1; i++)
{
if(syntax[i]==char)
{
count++;
}
}
return console.log(syntax + " : counting the character : " + char + " : is equal with : " + count);
}
console.log(countChars(prop, "p"));
/*__________________________________________________________________________*/
function countBs_rewrite(prop){
return countChars(prop, "B");
}
console.log(countBs_rewrite(prop));