-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
158 lines (126 loc) · 3.14 KB
/
practice.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
// var message = "Hello Worlds!";
// console.log(message);
// Variables
/*
var x = "Hey ";
var y = "10";
console.log(x);
console.log(y);
for (var i = 0; i < y; i++) {
console.log(x);
}
y = "is for horses!";
console.log(x+y);
*/
// Data Types
// numbers
// var interger_num = 1,
// float_num = 1.23;
// console.log(interger_num + float_num);
// // strings
// var message = "Let's learn JavaScript!";
// console.log(message);
// // booleans
// var is_cool = true;
// console.log(is_cool + " dat")
// // arrays
// var my_stuff = [interger_num, message, is_cool];
// console.log(my_stuff);
// // hashes
// var capitals = {
// LA: "Baton Rouge",
// TX: "Austin",
// GA: "Atlanta"
// };
// console.log(capitals["LA"]);
// null and undefined (JS has two concepts of emptiness)
// variables that haven't been initialized are undefined
// var x;
// console.log(x); //undefined
// // but they aren't null
// console.log(x === null); // false
// // unless you make them null
// x = null;
// console.log(x);
// console.log(x === null);
// // or use 'type coercion' - 2 equal signs will change the value
// console.log(x == undefined);
// console.log(x);
// // This variable is in the global scope
// var x = "I'm a global variable called 'x'";
// // console.log(x);
// // Defining a function called someFunction
// function someFunction(){
// // This variable only exists inside this function
// var y = "I'm a local variable called 'y'";
// console.log(x);
// console.log(y);
// // This can be bad, this modifies the global variable within this function
// x = "I'm now a bad local variable called 'x'"; //
// }
// // Calling someFunction
// someFunction();
// // console.log(y); // will cause errors
// console.log(x);
// Operators
// var x = 10,
// y = 5;
// console.log(x + y);
// console.log(x - y);
// console.log(x * y);
// console.log(x / y);
// console.log(x % y);
// console.log(y - x);
// console.log(x > y);
// console.log(x < y);
// console.log(x === y);
// console.log(x !== y);
// var a = true,
// b = false;
// console.log(a && b); // ampersands make and
// console.log(a || b); // pipes make or
// Conditionals
// var x = 1,
// y = 5;
// if (x > y){
// console.log("x is greater than y")
// } else if (x < y){
// console.log("x is less than y")
// } else {
// console.log("x is equal to y")
// }
// Loops
var numbers = [1, 2, 3, 4, 5];
// C-style JS Loop
// for (var i = 0; i < numbers.length; i++){
// console.log(numbers[i]);
// }
// // for each loop
// numbers.forEach(function (element){
// console.log(element);
// });
// While loop
// var i = 0;
// while (i < numbers.length){
// console.log(numbers[i]);
// i++;
// }
// Functions
// A Function with explicit arguments
// function sumExplicity(a, b){
// console.log(a + b);
// }
// sumExplicity("There are ", 10);
// A Function with implicit arguments
function sumImplicity(){
var total = 0;
for (var i = 0; i < arguments.length; i++){
total += arguments[i];
}
// console.log(total);
return total;
}
var x = sumImplicity(10, 5, 20, " that's a lot", 10, 40, " 10");
console.log(x);
var y = sumImplicity(x, sumImplicity(7,8));
console.log("y is equal to " + y);