-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_intermediate.js
More file actions
277 lines (223 loc) · 6.83 KB
/
javascript_intermediate.js
File metadata and controls
277 lines (223 loc) · 6.83 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
// ...Intermediate JavaScript Learning...
// Dice roll numbers generator.
// var n = Math.random();
// n = n * 6;
// n = Math.floor(n) + 1;
// console.log(n);
// // Love Calculator.
// alert("Welcome to The Love Calculator: ");
// var firstPerson = prompt("Enter the name of 1st person: ");
// var secondPerson = prompt("Enter the name of 2nd person: ");
// var n = Math.random();
// n = n * 100;
// n = Math.floor(n) + 1;
// if(n > 70) {
// alert("Your love score is " + n + " %" + " You love each other like Kanye loves Kanye.");
// }
// if (n > 30 && n <= 70) {
// alert("The Love Score between " + firstPerson + " & " + secondPerson + " is " + n + " %");
// }
// if (n <= 30) {
// alert("The Love Score between " + firstPerson + " & " + secondPerson + " is " + n + " %" + " You go together like oil and water.");
// }
// else {
// alert("Your love score is " + n);
// }
// var a = 1;
// var b = "1";
// if (a === b) {
// console.log("Yes.");
// } else {
// console.log("No."); // This one executes.
// }
// var a = 1;
// var b = "1";
// if (a == b) {
// console.log("Yes."); // This one executes.
// } else {
// console.log("No.");
// }
// BMI Calculator.
// function bmiCalculator (weight, height) {
// var bmi = weight / (height * height);
// if(bmi < 18.5) {
// return "Your BMI is " + bmi + ", so you are underweight.";
// }
// if(bmi >= 18.5 && bmi <= 24.9) {
// return "Your BMI is " + bmi + ", so you have a normal weight.";
// }
// if(bmi > 24.9) {
// return "Your BMI is " + bmi + ", so you are overweight.";
// }
// }
// Leap Year Program.
// function isLeap(year) {
// if (year % 4 == 0) {
// if(year % 100 == 0) {
// if(year % 400 == 0) {
// console.log("Leap year.")
// } else {
// console.log("Not leap year.");
// }
// } else {
// console.log("Leap year.")
// }
// } else {
// console.log("Not leap year.");
// }
// }
// var guestName = prompt("Enter the name of guest which you want to check in the array: ");
// var guestList = ["Usama", "Daniyal", "Bilal", "Ahmed", "Hassan"];
// if (guestList.includes(guestName)) {
// alert("Welcome!");
// } else {
// alert("Sorry, next time :)");
// }
// console.log(guestList[1]);
// console.log(guestList.includes("Daniyal"));
// FizzBuzz Problem.
// var output = [];
// var n = 1;
// function fizzBuzz() {
// if (n % 3 == 0 && n % 5 == 0) {
// output.push("FizzBuzz");
// }
// else if (n % 3 == 0) {
// output.push("Fizz");
// } else if (n % 5 == 0) {
// output.push("Buzz");
// } else {
// output.push(n);
// }
// n += 1;
// console.log(output);
// }
// fizzBuzz();
// function whosPaying(names) {
// var n = Math.random();
// n = n * names.length;
// n = Math.floor(n);
// var name = names[n];
// return name + " is going to buy lunch today!";
// }
// whosPaying(["Angela", "Ben", "Jenny", "Michael", "Chloe"]);
// While Loop.
// var i = 1;
// while (i <= 2) {
// console.log(i);
// i++;
// }
// function milkShake() {
// var i = 99;
// var word = "bottles";
// while (i >= 0) {
// var oneDown = i - 1;
// if (i > 1) {
// console.log(i + " " + word + " of Milkshake on the wall, " + i + " " + word + " of Milkshake.");
// console.log("Take one down and pass it around, " + oneDown + " " + word + " of Milkshake on the wall.");
// } else if (i == 1 && oneDown == 0) {
// word = "bottle";
// console.log(i + " " + word + " of Milkshake on the wall, " + i + " " + word + " of Milkshake.");
// console.log("Take one down and pass it around, " + "no more bottles of Milkshake on the wall.");
// } else {
// oneDown = 99;
// console.log("No more bottles of Milkshake on the wall, " + "no more bottles of Milkshake.");
// console.log("Go to the store and buy some more, " + oneDown + " bottles of Milkshake on the wall.");
// }
// console.log("");
// i--;
// }
// }
// var output = [];
// var n = 1;
// function fizzBuzz() {
// while (n < 101) {
// if (n % 3 == 0 && n % 5 == 0) {
// output.push("FizzBuzz");
// }
// else if (n % 3 == 0) {
// output.push("Fizz");
// } else if (n % 5 == 0) {
// output.push("Buzz");
// } else {
// output.push(n);
// }
// n += 1;
// }
// console.log(output);
// }
// fizzBuzz();
// var output = [];
// function fizzBuzz() {
// for (var n = 1; n < 101; n++) {
// if (n % 3 == 0 && n % 5 == 0) {
// output.push("FizzBuzz");
// }
// else if (n % 3 == 0) {
// output.push("Fizz");
// } else if (n % 5 == 0) {
// output.push("Buzz");
// } else {
// output.push(n);
// }
// }
// console.log(output);
// }
// fizzBuzz();
// Fibonacci Series Program. My Solution.
// function fibonacciGenerator (n) {
// var arr = [];
// for(var i = 0; i < n; i++) {
// if (i == 0 || i == 1) {
// arr.push(i);
// } else {
// var sum = arr[i - 2] + arr[i - 1];
// arr.push(sum);
// }
// }
// return arr;
// }
// 2nd Method. Instructor's Solution.
// function fibonacciGenerator (n) {
// var output = [];
// if(n == 1) {
// output = [0];
// } else if (n == 2) {
// output = [0, 1];
// } else {
// output = [0, 1];
// for(var i = 2; i < n; i++) {
// output.push(output[output.length - 2] + output[output.length - 1]);
// }
// }
// return output;
// }
// High Order Functions & Callback.
// Function that takes function as an argument is High order function.
// You can use callbacks in the case of timing consuming scenarios like API calling and if the
// certain task is achieved then we will call the callback function.
// The function which needs to be called is the callback function.
// Callbacks used very widely in javascript earlier versions and to overcome callback hell
// scenario the concept of promises were introduced. Promises helps in writing clean code.
// function add(a, b, cb) {
// let result = a + b;
// cb(result);
// }
// function add(a, b, cb) {
// let result = a + b;
// cb(result);
// return () => console.log(result);
// }
// let resultFunc = add(2, 4, () => {});
// console.log(typeof(result));
// resultFunc();
// function showResult(result) {
// console.log(result);
// }
// add(2, 2 , function(val) {
// console.log(val);
// });
// add(3, 2, (val) => console.log(val));
// add(3, 2, val => console.log(val)); val is an argument
// add(400, 10, val => console.log(val));
// add(5, 2, showResult);