-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyourPlayground.js
458 lines (369 loc) · 10.3 KB
/
yourPlayground.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// *Variables(var, const,let)
// *NEVER use var
// let uname = "Peter Pan";
// console.log(uname);
// let sentence = "How are you doing today";
// * user input
// /** //*data types (string, numbers)
// * *numbers 👉🏾 1,5,10,100,2.5
// * *strings 👉🏾 'hello', "What is up"
// * *arrays 👉🏾 []
// * *objects 👉🏾 {}
// * *boolean 👉🏾 true/false
// */
// /** //* Math Operations
// * *Mutiply *
// * *Divide /
// * *Exponents **
// * *Modulo/Remainder % 5%2 = 1
// * *Add +
// * *Subtract -
// */
// /** //* Math.Methods
// * *Floor-Rounds Up
// * *Ceil-Rounds Down
// * *Random- give you a random number-----Math.random()*3___get number between 3
// */
// // *Baby weather app (CONDITIONALS)
// /** //TODO if rain 'grab your umbrella '
// * *else 'Wear your sunglasses'
// */
// // let weather = prompt("How is the weather?");
// // if (weather == "rainy") {
// // console.log("Grab your Umbrella ☔️");
// // } else {
// // console.log("Wear your sunglasses 😎");
// // }
// // *Functions
// // *function is called 'sayMyName'
// // * and it has 0 arguments
// // *does: it logs out your name to console
// function sayMyName() {
// // console.log("Leah");
// }
// // sayMyName();
// /**
// * * Function is called 'sayMyName2'
// * * @param uname
// * *does: logs name to console log
// */
// function sayMyName2(uname) {
// // console.log("uname");
// }
// //sayMyName2();
// function greeting(uname) {
// greet = `hi ${uname}, nice to meet you!`;
// // console.log(greet);
// }
// // greeting("Zac");
// // function sum(a, b) {
// // return a + b;
// // }
// // num1 = sum(1, 2);
// // console.log(num1);
// function calculateFoodTotal(food, tip) {
// const tipPercentage = tip / 100;
// const tipAmount = food * tipPercentage;
// const total = sum(food, tipAmount);
// return total;
// }
// // console.log(calculateFoodTotal(300, 20));
// // *ES6
// // Arrow Functions =>
// // Arrow Function w/ explicit return
// const sumArrow4 = (a, b) => {
// return a + b;
// };
// // Arrow Function w/ implicit return
// // Automatically returns -- NO {}
// const sumArrow3 = (a, b) => a + b;
// // console.log(sumArrow3(10, 50));
// *Arrays
//// const groceries = ["bananna", "apple", "blueberry", "pear"];
//console.log(groceries);
// *Grab the 2nd index
//console.log(groceries[2]);
// *Array Methods (slice, push, indexOf, length)
////groceries.push("cookie", "orange");
// //console.log(groceries);
// *Array Slice
// *Start from 0 INCLUSIVE and UP to 6 [0,1,2,3,4,5]
//ToDo console.log(groceries.slice(0, 6));
//*Start from 2 UP to #
//ToDo console.log(groceries.slice(1, 4));
// * Array indexOf
//* Gives you the index value of the array
// TODO console.log(groceries.indexOf("apple"));
// * Array length
// * Show you number of items in an array
//console.log(groceries.length);
// * OBJECTS
// *Property-- One of the key:value pairs
// const person = {
// // * <---Object
// name: "Leonardo",
// shirt: "White",
// };
// * dot notation
// console.log(person.name); // .notation
// console.log(person["name"]); // [""]notation
//* bracket notation
// console.log(person.name);
// console.log(person.shirt);
// person.phone = "1-222-234-4444";
// console.log(person["phone"]);
// console.log(person);
// const person2 = {
// name: "Zac",
// shirt: "White",
// };
// console.log(person2["shirt"]);
// * Has 2 Arguments
// *
// function welcomeMsg(name, shirt) {
// return `Welcome ${name}, I like your ${shirt} shirt!!`;
// }
// *ES6--ARROW FUNCTIONS=>(2 Arguments)
//*OBJECT
//*Template Literals``
// * METHODS-- a property containing a function--Math.floor()
const introducer = (name, shirt) => {
const person = {
name: name,
shirt: shirt,
assets: 100000,
liability: 3000,
// !networth:(assets-liability),-- will NOT work since EVERYTHING is happening at the same time
//TODO you can create functions within OBJECTS
networth: function () {
//*METHOD
return this.assets - this.liability;
},
};
const intro = `Hi, my name is ${person.name} and the color of my shirt is ${
person.shirt
} and my networth is $ ${person.networth()}`; //* call is by using--property.METHOD()
return intro;
};
console.log(introducer("zac", "white"));
//*FOR LOOPS
//?Print All Data
// const fruits = [
// "bananna",
// "apple",
// "blueberry",
// "pear",
// "bananna",
// "apple",
// "blueberry",
// "pear",
// "bananna",
// "apple",
// "blueberry",
// "pear",
// ];
// ? (let i=0;) <------INDEX
// ? (i < fruits.length;) <------CONDITION for the loop to run 0 for ALL fruits
// ? (i++); <------Adds 1 to i each time it runs
// for (let i = 0; i < fruits.length; i++) {
// console.log(fruits[i]); //*<----(i,array.length;i++),,appends number before value
// }
// *fruits <------is the ARRAY
// *fruit <-------When looping it changes with the index[] until it has printed the WHOLE ARRAY
// for (const fruit of fruits) {
// console.log(fruit.length);
// }
const numbers = [1, 2, 3, 4, 5, 6];
// *sum up all numbers in ARRAY
// for (let i = 0; i < numbers.length; i++) {
// console.log(numbers[i]);
// }
//*Function for doubling numbers in an ARRAY
const double = () => {
let result = []; // * <---Temporary Array
for (const number of numbers) {
// * A loop
// console.log(number * 2); //*times the variables in the ARRAY by 2
result.push(number * 2); //*Get a NEW ARRAY an print to console with result
}
return result; // * <-----Returning the Array
};
//console.log(double([1, 2, 3, 4, 5, 6, 7])); //*<----Make sure to put outside on LOOP so it doesnt run again
const letterCounter = (phrase) => {
phrase.length;
//* Counter
let result = 0;
for (const index in phrase) {
console.log(Number(index) + 1);
result = Number(index) + 1;
}
return { result };
};
//const phrase = "what color shirt are you wearing?";
//console.log(letterCounter(phrase));
// [1, 2, 3, 4]; // 10
// result = 0;
// result = 1;
// result = 3;
// result = 6;
// result = 10;
const sumArray = (numbers) => {
//<----- Arrow Function sumArray
let result = 0; //<----Give result the value of 0 instead of undefined
// for loop
for (const number of numbers) {
//*<-----Looping through an printing number
console.log(number);
result += number;
}
return { result };
};
////const nums = [1, 2, 3, 4, 5]; //<---nums refer to the array-- Global Frame(variables can be grabbed from anywhere)
////console.log(sumArray(nums)); //<------
// * FIND MAX NUMBER
const max = (numbers) => {
let result = numbers[0];
// * Loop
for (const number of numbers) {
if (number > result) {
result = number;
}
}
return { result };
};
console.log(max([1, 2, 3, 4, 5, 20, 45, 43, 2234, 34, 4653, 34]));
const letterFrequency = (phrase) => {
// * frequency('haha')---> {'h':2, 'a':2}
console.log(phrase);
// ToDo make a 'freq' object {}
let frequency = {};
for (const letter of phrase) {
//* Check if letter exists in frequency
if (letter in frequency) {
frequency[letter] += 1;
} else {
frequency[letter] = 1;
}
}
//* increment the value by +1
//* otherwise, set the value to 1
return frequency;
};
console.log(
letterFrequency("Hey, how is your day going? It's sunny and hot here.")
);
// * INCREMENTAL OPERATORS
// ? ++, --, +=
// * wordFrequency ('lol what lol') 👉🏾 {'lol':2, 'what':1}
const wordFrequency = (phrase) => {
// let frequency = {}; //* OBJECT
const words = phrase.split(" "); // * Turn it into and ARRAY -- (" ") seperated by spaces
/** //!CAN BE REPLACED
* //* with letterFrequency()
for (const word of words) {
console.log(word);
if (word in frequency) {
frequency[word] += 1;
} else {
frequency[word] = 1;
}
}
*/
return letterFrequency(words);
};
// const userInput = prompt("Write your message");
// console.log(wordFrequency(userInput));
// * Higher Order Function
// !?!? ARRAY METHODS
// * Map - Loops and Returns an Array
// * Filter - Loops and Returns and Array with matching conditions
// * Reduce
// * Map() ---Loop Through the Array an log to the console
// [1, 2, 3, 4].map((number) => console.log(number)); //* <-----Implicit Return (automatically invoked)
const doubleMap = (numbers) => {
return numbers.map((number) => number * 2);
};
// let result = [1, 2, 3, 4].map((number) => number * 2); //* <----Shorter way to double the vairables in the ARRAY
// console.log(result);
//* Filter([1,2,3,4,5,6], 3) 👉🏾 [4,5,6]
// const filter = (numbers, greaterThan) => {
// let result = [];
// for (const number of numbers) {
// if (number > greaterThan) {
// result.push(number);
// }
// }
// return result;
// };
const numb5 = [1, 2, 3, 4, 5, 6];
const actors = [
{ name: "Johnny", netWorth: 150000000 },
{ name: "Amber", netWorth: -6000000 },
{ name: "Matt", netWorth: 170000000 },
{ name: "Brad", netWorth: 300000000 },
{ name: "Leonardo", netWorth: 260000000 },
];
console.log(numb5.filter((num) => num > 4 || num < 2));
// let result = actors.filter((actor) => actor.netWorth > 10);
// let names = result.map((actor) => actor.name).join(", ");
// console.log(actors.reduce((a, b) => a + b.netWorth, 0)); //* SUM UP the actors NETWORTH
// ! Example of conditionals for a user
/** //* if (userIsAuthenticated && userIsPayingMember){
* *
* } else {
*
* * }
*/
//document.getElementById("playgroundD").innerHTML = `<h1>${names}</h1>`;
//* Reduce
// * sum all of the net worths
// * SUM: think REDUCE!!
function sum(a, b) {
// * CAN USE WITHIN REDUCE
return a + b;
}
const nums = [1, 2, 3];
const result = nums.reduce(sum);
// !OR
// ? const nums2 = [1, 2, 3];
// ? const result2 = nums.reduce((a, b) => a + b);
console.log(result);
//* Select Random Element from an ARRAY
//* randomFruit([1,2]) 👉🏾 2
//* randomFruit([1,2]) 👉🏾 1
const randomFruit = (fruits) => {
const randomNumber = Math.floor(Math.random() * fruits.length);
return fruits[randomNumber];
};
fruits = [
"bananna",
"apple",
"blueberry",
"pear",
"bananna",
"apple",
"blueberry",
"pear",
"bananna",
"apple",
"blueberry",
"pear",
];
console.log(fruits[0]);
console.log(randomFruit(fruits));
// * if else if else
// * rainy(1), sunny (-1), overcast (0)
const weatherScorer = (weather, weather2) => {
let score;
if (weather == "rainy" && weather2 == "overcast") {
score = 2;
} else if (weather == "rainy") {
score = 1;
} else if (weather == "sunny") {
score = -1;
} else {
score = 0;
}
return score;
};
console.log(weatherScorer("rainy", "overcast"));