-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASIC - FUNCTIONS - (A) - basic.js
More file actions
132 lines (102 loc) · 2.9 KB
/
Copy pathBASIC - FUNCTIONS - (A) - basic.js
File metadata and controls
132 lines (102 loc) · 2.9 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
/*
!!FUNCTIONS - BASIC!!
> You can put your functions anywhere in your JavaScript file
> Remember, when you return from a function, the function stops executing,
so any lines of code after the return are ignored
> JavaScript actually makes two passes over your page:
1) in the first pass it reads all the function definitions,
2) and in the second it begins executing your code.
>> So, that allows you to place functions anywhere in your file
> It’s just good programming practice to use local variables unless you absolutely need globals
>> do you need variables that are accessable for multiple functions or only one function
*/
/*
GOOD CODING PRACTICES:
1) Global variables, right at the TOP!
2) Functions like to sit together.
3) Let your local variables be declared at the TOP of the function they’re in.
*/
/*
BASIC CODE:
*/
function functionName(argument1, argument2) {
/*execute code here....*/
}
functionName("Marko", 34);
/*
EXAMPLE:
*/
/*cupa & tea = parameters*/
function makeTea(cups, tea) {
document.write("Brewing " + cups + " cups of " + tea);
}
var guests = 3;
/*guests & Earl Grey = arguments*/
makeTea(guests, "Earl Grey");
// Brewing 3 cups of Earl Grey
/*
1) EXPERIMENT #1: what happens when we don’t pass enough arguments
>> each parameter that doesn’t have a matching argument is set to undefined
2) EXPERIMENT #2: what happens when we pass too many argments
>> JavaScript just ignores the extra
3) EXPERIMENT #3: what happens when we have NO parameters
>> No worries, many functions have no parameters
*/
1)
function makeTea(cups, tea) {
console.log("Brewing " + cups + " cups of " + tea);
}
makeTea(3);
// Brewing 4 cups of undefined
2)
function makeTea(cups, tea) {
console.log("Brewing " + cups + " cups of " + tea);
}
makeTea(3, "Earl Grey", "hey ma!", 42);
// Brewing 3 cups of Earl Grey
3)
function barkAtTheMoon() {
console.log("Woooooooooooooo!");
}
barkAtTheMoon();
// Woooooooooooooo!
/*
EXAMPLe - RETURN VALUES
*/
function animalSounds(animal) {
/*Always declare empty local variable*/
var message;
/*This means variable is global - do not do this if meant to be local variable*/
message;
if(animal == "Dog") {
message = "Woof";
} else if(animal == "Cat") {
message = "Miau";
} else {
message = "Groargh";
}
return message;
}
document.write(animalSounds("Dog"));
// Woof
/*
EXAMPLE - COMPLICATED
> follow the number logic
*/
function calculateArea(r) { // 3
var area;
if(r <= 0) { // 4
return 0; // 5
} else { // 6
area = Math.PI * r * r; // 7
return area; // 8
}
}
var radius = 5.2; // 1
var theArea /*9*/ = calculateArea(radius); /*2*/
console.log("The area is: " + theArea); // 10
// The area is: 84.94866535306801
/*
GLOBAL & LOCAL VARIABLES
> standard global is outside of function, local is inside function
*/