-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.js
353 lines (286 loc) Β· 8.61 KB
/
basics.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
// Commented out to avoid cluttering the console
// Console log
console.log("Binge JavaScript...\nThe output is: ");
// variables
let name = "Raihan";
let age = 26;
// console.log(name, age);
// sentences
sentence1 = "Hello World, I am " + name + " and I am " + age + " years old.";
// console.log(sentence1);
// operators
const prompt = require("prompt-sync")();
// wrap in number to avoid typecasting
let food = Number(('How much was the food: '));
let tipPercent = Number(('What is the tip percentage: ')) / 100;
tipAmount = food * tipPercent;
total = food + tipAmount
// console.log("The tip amount is $" + tipAmount);
// console.log("The total is $" + total);
// data types
// Ctrl + I for emoji
// Time: 24:18
// console out random number with Math.random() and Math.floor()
let GenRandomNum = (Math.floor(Math.random() * 10));
//console.log(GenRandomNum);
// var - variable - can change (not recommended)
// const - constant (cannot be changed)
// let - let (value can be changed)
// weather app (conditional)
// if rain "Grab an umbrella β" else "Wear a sunglasses πΆ"
// conditional operator (if, else if, else)
// ==, ===, !=, !==, >, <, >=, <=
// let weather = prompt("How is it outside: ")
// if (weather == "rain") {
// console.log("Grab an umbrella β");
// } else {
// console.log("Wear a sunglasses πΆ");
// }
// 40:00
// functions - reusable code
function AskName() {
let name = prompt("What is your name: ");
console.log("Hello " + name);
}
// calling the function
// AskName()
// dynamic function
function AskNameInc(name) {
console.log("Hello " + name);
}
// AskNameInc("Raihan");
// template literals - backtick (`)
function StartFunc(name){
greet = `Hello ${name}`;
console.log(greet);
}
// StartFunc("Testing")
// return - return a value from a function
function Addition(num1, num2){
return num1 + num2
}
Add1 = Addition(1, 2)
// console.log(Add1)
// tip calculator with function
function TipCalc(food, tip){
const tipPercent = Number(tip / 100)
const tipAmount = food * tipPercent
const total = Addition(food, tipAmount)
return total
}
// console.log(TipCalc(300, 20))
// ES6 - arrow functions
// () => {}
// sum with arrow function and implicit return
const sumArrow = (a, b) => a + b
// console.log(sumArrow(1, 2));
// Completed Function exercise (01:11:40)
// Arrays
const fruits = ["π", "π", "π", "π", "π", "π", "π"]
// 2nd index
// console.log(fruits[1]);
// array push
fruits.push("π")
// console.log(fruits)
// array splice
// console.log(fruits.slice(0, 2))
// console.log(fruits.slice(2, 5))
// console.log(fruits.slice(1, 4))
// console.log(fruits.slice(3, 5))
// array indexOf
// console.log(fruits.indexOf("π"))
// array length
// console.log(fruits.length)
// objects{}
// key: value pairs
const person1 = {
name1: "Raihan",
age1: 26,
favoriteFood1: "Apple",
favoriteColor1: "Yello"
}
// console.log(person.name1, person.age1, person.favoriteFood1, person.favoriteColor1)
// dot notation
// console.log(person.name1)
// bracket notation
// console.log(person["favoriteFood1"])
// assign object
person1['phone1'] = "123-456-7890"
// console.log(person.phone1)
const person2 = {
name2: 'Qazi',
shirt2: 'black'
}
person2['YT2'] = 'Clever Programmer'
// console.log(person2['YT2'])
// ES5 arrow function with arg, object and template literals and methods (this)
const introduction = (name, shirt, age, phone) => {
const personN = {
name: name,
shirt: shirt,
age: age,
phone: phone,
assets: 100000,
liabilities: 50000,
netWorth: function() {
return this.assets - this.liabilities
}
}
const intro = `Hi, my name is ${personN.name} and my the color of my shirt is ${personN.shirt}. \nI am ${personN.age} years old and my phone number is ${personN.phone}.\nMy networth is $${personN.netWorth()} USD`
return intro
}
// console.log(introduction("Raihan", "black"))
// console.log(introduction("Qazi", "white", "26", "123-456-7890"))
// for loops
// for (let i = 0; i < fruits.length; i++) {
// console.log(i, fruits[i])
// }
for (const fruit of fruits){
// console.log(fruit)
}
const doubleF = (num3) => {
let doubled = []
// let squared = []
for (const num of num3){
doubled.push(num * 2)
// squared.push(num ** 2)
}
return doubled
// return squared
}
// console.log(doubleF([1, 3, 6, 9, 12, 15]))
// letter counter
const letterCounter = (phrase) => {
let result = 0;
// for in and of
for(const index in phrase){
console.log(Number(index) + 1)
result = Number(index) + 1
}
return { result }
// return { result: phrase.length }
}
// const phrase = 'How many letters in this phrase?'
// const phrase = prompt("Enter a phrase: ")
// console.log(letterCounter(phrase))
// add numbers in array
const addArray = (AR) => {
// increment the result
let sumres = 0;
for (const sr in AR){
sumres += AR[sr]
}
return { sumres }
}
// get the max number in array
const max = (num4) => {
let res4 = num4[0]
for (const num of num4){
if (num > res4){
res4 = num
}
}
return { res4 }
}
// console.log(max([1, 3, 6, 9, 12, 15]))
// calculate frequency of letters in a phrase
const letterFreq = (phrase) => {
let result = {}
for (const letter of phrase){
if (letter in result){
result[letter]++
} else {
result[letter] = 1
}
}
return { result }
}
// const phrase = prompt("Enter a phrase: ")
// console.log(letterFreq('This is another test'))
// word frequency in a phrase
const wordFreq = (phrase) => {
const word = phrase.split(' ')
// calling similarly coded function with word
return letterFreq(word)
// hard coded
// let result = {}
// for (const word of phrase.split(' ')){
// if (word in result){
// result[word]++
// } else {
// result[word] = 1
// }
// }
// return { word:result }
}
// const phrase = prompt("Enter a phrase: ")
// console.log(wordFreq(phrase))
// higher order functions - function that takes a function as an argument
// map - loops and returns a new array implicit return
let num5 = [1, 2, 3, 4, 5].map(Number => Number * 2)
// console.log(num5)
const doubleMap = (num6) => {
return num6.map(Number => Number * 2)
}
// console.log(doubleMap([1, 2, 3, 4, 5]))
// filter - loops and returns an array with matching conditions
// filter function
const filterF = (Fnum, FGT) => {
let Fresult = []
for (const num of Fnum){
if (num > FGT){
Fresult.push(num)
}
}
return Fresult
}
//console.log(filterF([1, 2, 3, 4, 5], 2))
// greater than 3 using filter method
const greaterThan3 = (num7) => {
return num7.filter(Number => Number > 3)
}
// console.log(greaterThan3([1, 2, 3, 4, 5]))
// && - and
// const AND = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// console.log(AND.filter(AND => AND > 3 && AND < 7))
// || - or
// const OR = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// console.log(OR.filter(OR => OR > 10 || OR < 7))
// show richest actors
const actors = [
{name: 'Tom Cruise', age: 56, netWorth: 1500000},
{name: 'Robert De Niro', age: 25, netWorth: 2500000},
{name: 'Al Pacino', age: 66, netWorth: 3000000},
{name: 'Marlon Brando', age: 75, netWorth: 4000000},
{name: 'Jack Nicholson', age: 45, netWorth: 50000},
{name: 'Daniel Day-Lewis', age: 60, netWorth: 6000000},
{name: 'Dustin Hoffman', age: 79, netWorth: 7000000},
]
let AR = actors.filter(actor => actor.netWorth > 3000000)
// console.log(AR)
let AA = AR.map(actor => actor.name).join(', ')
// console.log(AA)
// playground.innerHTML += `<h1>${AR[1].name}</h1>`
// reduce - loops and returns a accumulator
// summing an array of numbers with reduce
const Rsum = (a, b) => a + b
const Rmul = (a, b) => a * b
const rNum = [1, 2, 3, 4]
const RN = rNum.reduce(Rsum) // called in the function from above
const RM = rNum.reduce(Rmul) // called in the function from above
// console.log(RN)
// console.log(RM)
// DOM manipulation
// GO to Exercices to see DOM.js (https://replit.com/@raihanrms/DOM-Manupilation)
// small exercise for rock paper scissors
// select random element from array
const randomFruits = (Flist) => {
const randomGen = Math.floor(Math.random() * Flist.length)
console.log(randomGen)
return Flist[randomGen]
}
const Flist = ['π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π']
// console.log(randomFruits(Flist))
// conditional statements
// navigate to BingeJS > Exercises > weatherScore.js
// SuperHero API is not working...