-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsFunctions.html
More file actions
390 lines (252 loc) · 9.32 KB
/
JsFunctions.html
File metadata and controls
390 lines (252 loc) · 9.32 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<Title> Javascript.info/function-basics</Title>
</head>
<body>
<script>
/*
// Function Declaration
function showMessage() { // function name(parameter1, paramenterN) {
alert ('Hello world!'); // ...body...
}
showMessage();
showMessage();
// Local Variables
function showMessage() {
let message = 'Hello, I\'m JavaScript!';
alert( message);
}
showMessage(); // Hello, I'm JavaScript!
alert( message ); // <-- Error! the variable is local to the function
*/
/*
// Outer Variables
let userName = 'John';
function showMessage() {
let message = 'Hello, ' + userName;
alert(message);
}
showMessage(); // Hello, John
// function has full access to outer variables, which it can modify as well
*/
/*
let userName = 'John';
function showMessage() {
userName = 'Bob'; //(1) changed the outer variable
let message = 'Hello, ' + userName;
alert(message);
}
alert(userName); // John, before function call
showMessage();
alert (userName); // Bob, modified by function
// variables declared outside of functions, such as the outer userName, are 'Global'.
// Global variables are visible from ANY fuction (unless shadowed by locals)
// Min the use of global variables, primary for project level-data if at all
// Modern code has few/no globals
/*
// Parameters -- Pass arbitrary data to functions
function showMessage(from, text) {
alert (from + ': ' + text);
}
showMessage('Ann', 'Hello!'); // Ann: Hello! (*)
showMessage('Ann', 'What\'s up?'); // Ann: What's up? (*)
// given values (*, **) are copied to local variables [from, text] --> function then uses them
*/
/*
// Parameters 2
function showMessage(from, text) {
from = '*' + from + '*'; //makes "from" look nicer
alert (from + ': ' + text);
}
let from = "Ann";
showMessage(from, "Hello"); // *Ann*: Hello
// value of "from" remains unchanged, function modifies a local copy
alert (from); // Ann
// values as function parameters also called arguments
// parameter = variable listed inside the () in the function declar (decl time term)
// argument = value passed to the function when called (call time function)
// declare functions with listed params, call them by passing arguments
*/
/*
// Default Values
// when values are not defined, default values (undefined, or user set) will return
// Ex 1 showMessage(from, text) as above > *Ann*: undefined since a value for text was not passed
function showMessage(from, text = 'no text given') {
alert( from + ': ' + text );
}
showMessage('Ann'); // Ann: no text given
// 'no text given' is passed as a string but more complex series are also possible
//Ex 2
// function showMessage(from, text = anotherFunction()) {
//anotherFunction() on executes if no text is given > result populates the value of text
*/
/*
// Alt default params
function showMessage(text) {
// ..
if (text === undefined) { // if param is missing
text = 'empty message';
}
alert(text);
}
showMessage(); // empty message
*/
/*
//alt || operator
function showMessage(text) {
// if text is undef. or otherwise falsy, set it to 'empty'
text = text || 'empty';
}
*/
/*
// modern JS engines support nullish coalescing operator ?? > better when most falsy values such as 0 should be considered 'normal'
function showCount(count) {
// if count is undefined or null, show 'unknown'
alert(count ?? 'unknown')
}
showCount(0); // 0
showCount(null); // unknown
showCount(); //unknown
*/
/*
// Return values back into the calling code
function sum(a, b) {
return a +b;
}
let result = sum(1, 2);
alert( result ); // 3
*/
/*
function checkAge(age) {
if (age >= 18) {
return true;
} else {
return confirm('Do you have parental permission to continue?');
}
}
let age = prompt('How old are you?', 18);
if (checkAge(age) ) {
alert( 'Access Granted' );
} else {
alert( 'Access Denied');
}
// no input still returns 'Do you have permission...?'
// THis doesnt actually work... Idk
// supposed to represent this:
// Using return without a value, causing the function to exit imeediately
function showMovie(age) {
if ( !checkAge(age) ) {
return;
}
alert ('Now Playing'); // (*)
}
*/
/*
function doNothing() {
return;
}
alert( doNothing() ===undefined); // true
//an empty return is same as return undefined
*/
/*
// return
// (some + thing + helllla + long *f(a) + f(b))
// !! dont do this - JS sees this as an empty return
return (
some + thing + helllla
+ long *f(a) + f(b)
)
// Do this instead
*/
//Naming a function
// should usually be a verb, concise, accurate, and descriptive
// how would a stranger read it?
// eg. get...returns a function
// calc...calculate someting
// create...create something
// check... check something and return a boolean
/*
showMessage(...) // Shows a message
getAge(...) // returns the age (gets somehow)
calcSum(...) // calculates a sum and returns result
createForm(...) // creates a form (usually returns it)
checkPermission(...) // checks a permission, returns true/false
*/
// Functions == Comments
// a large function is often best split into multiple smaller functions
// this makes functions easier to debug and test
// additionally this makes the function a great comment in itself
//Ex
/*
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert( i ); // a prime
}
}
function isPrime(n) {
for (let i = 2; i < n; i++) {
if ( n % i == 0) return false;
}
return true;
}
*/
/*
function checkAge(age) {
(age > 18 ? true : confirm('Did parents allow?'));
}
*/
/*
function checkAge(age) {
return (age > 18) || confirm('Did parents allow?');
}
*/
/*
// My attempt - write a min(a, b) functionn
function minValue(a, b) {
let c = Math.min(a, b);
alert(c);
}
// Solution 1 with 'if'
function min(a, b) {
if (a < b) {
return a;
} else {
return b;
}
}
// Solution 2 with '?'
function min (a, b) {
return a < b ? a : b;
}
*/
// My attempt - Write a function pow(x, n) **X to the power of N
function pow(x, n) {
return x **= n
}
// added to my solution based on solution below (input and display)
let x = prompt('x?', '')
let n = prompt('n?', '')
alert(pow(x, n))
/*
// solution, drawn out to show looping it seems
function pow(x, n) {
let result = x;
for (let i = 1; i < n; i++) {
result *= x;
}
return result;
}
let x = prompt("x?", '');
let n = prompt("n?", '');
if (n < 1) {
alert(`Power ${n} is not supported, use a positive integer`);
} else {
alert( pow(x, n) );
}
*/
</script>
</body>
</html>