-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanbox.js
898 lines (614 loc) · 17.8 KB
/
sanbox.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
// keywords
let age1 = 25;
let year =2024;
console.log(age1, year);
age1 = 35;
console.log(age1);
const points = 125;
console.log(points);
var score = 225;
console.log(score);
score = 325;
console.log(score);
// strings
console.log('Hello, world');
let email = '[email protected]';
console.log(email);
//string concatenation
let fisrtName = 'Selmy';
let lastName = 'Nshuti';
let FullName = fisrtName + ' ' + lastName;
console.log(FullName);
//getting characters
console.log(FullName[6]);
//string length
console.log(FullName.length);
//string methods
console.log(FullName.toUpperCase());
// let result = FullName.toLowerCase();
// console.log(result, FullName);
//argument
let index = email.indexOf('@');
console.log(index);
let count = fisrtName.indexOf('y');
console.log(count);
//common string methods
let eemail = '[email protected]'
let rresult = eemail.lastIndexOf('k');
console.log(rresult);
//slice
let rersult = eemail.slice(1,7);
console.log(rersult);
//substr
let reresult = eemail.substring(0,10);
console.log(reresult);
//replace
let ressult = eemail.replace('r', 'w');
console.log(ressult);
//mathematics
let radius = 10;
const pi = 3.14;
//division
console.log(10 / 2);
//order of operation B I D M A S
let resultt = 5* (10 - 3) **2;
console.log(resultt);
//show hand operation
// let likes = 10;
// likes++;
//this the same as likes = 10 + 1;
//likes--;
//this the same as likes = 10 - 1;
// likes += 10;
//this is the same as likes = 10 + 10;
// likes -= 5;
//this is the same as likes = 10 - 5;
// likes *= 2;
//this is the same as likes = 10 * 2;
// likes /=2;
//this is the same as likes = 10 / 2;
// console.log(likes);
//NaN - Not A Number
console.log(5 / 'Hello');
console.log(5 * 'Hello');
//template strings
const tittle = 'Best reads of 2024';
const Author = 'Nshuti';
const likes = 30;
//concatenation way
let result1 = 'The Blog called ' + tittle + ' by ' + Author + ' has ' + likes + ' likes ';
console.log(result1);
//template string way
let result2 = `The blog called ${tittle} by ${Author} has ${likes} likes`;
console.log(result2);
// creating html templates
// let html = `
// <h2>${tittle}</h2>
// <p>By ${Author}</p>
// <span>This bloh has ${likes} likes</span>
// `;
// console.log(html);
//Arrays
//strings
let Nshuti = ['Selmy','king','David'];
Nshuti[1] = 'Umwami'
console.log(Nshuti[1]);
//numbers
let selmy = [10,20,30];
console.log(selmy[2]);
//Array methods
let nshuti = ['selmy','king','david'];
console.log(nshuti.join('-'));
let result3 = nshuti.indexOf('david');
console.log(result3);
let result4 = nshuti.concat('styles','david');
console.log(result4);
let result5 = nshuti.push('king');
console.log(result5);
result5 = nshuti.pop();
//NaN and Undefined
let age2 = null;
console.log(age2, age2 + 3, 'The age is ${age2}');
//Booleans & Comparisons
console.log(true, false, 'true', 'false');
//methods can return booleans
let email1 = '[email protected]';
let result6 = email1.includes('@'); // this will return true because there's @ in the email
console.log(result6);
let names = ['selmy', 'nshuti', 'styles'];
let result7 = names.includes('king'); //this will return false because there's no king in the array
console.log(result7);
//comparison operators
let age3 = 25;
//equal to
console.log(age3 == 25); //true
console.log(age3 == 30); //false
//Not equal
console.log(age3 != 25); //false
console.log(age3 != 30); //true
//less and great than
console.log(age3 > 30); //false
console.log(age3 < 25); //true
//less and greater than or equal to
console.log(age3 >= 25);
console.log(age3 <= 20);
//comparison on strings
let names1 = 'nshuti';
console.log(names1 == 'nshuti'); //true
console.log(names1 == 'Nshuti'); //false because a name start with capital letter
console.log(names1 > 'crystal'); //true because 1st letter of 'nshuti' is greater than 1st letter of 'crystal'
console.log(names1 < 'Nshuti'); //false because in JavaScript LowerCase letter are greater than UpperCase letter
//type conversion
let score1 = '100';
score1 = Number(score1);
console.log(score1 + 1);
// for loops
// Example1
for(let i = 0; i < 5; i++){
console.log('in loop :', i);
}
console.log('loop finished');
//Example2
const names2 = ['selmy','nshuti','styles'];
for(i = 0; i < names2.length; i++){
console.log(names2[i]);
}
//Example3
const names3 = ['selmy','nshuti','styles'];
for(i = 0; i < names3.length; i++){
let html = `<div>${names3[i]}</div>`
console.log(html)
}
// While loops
//Example1
i = 0;
while(i < 5){
console.log('in loop', i);
i++;
}
//Example2
const names4 = ['selmy','nshuti','styles'];
i = 0;
while(i < names4.length){
console.log(names4[i]);
i++;
}
//do while loops
i = 2;
do {
console.log('Val of i is :',i);
i++;
}
while(i <5);
//if statements
//example1
const age = 25;
if(age > 20){
console.log('you are over 20 years old');
}
//example2
const names5 = ['selmy', 'nshuti','styles'];
if(names5.length > 2){
console.log("That's alot of ninjas");
}
//else if statements
const password = 'Rwanda@';
if(password.length >= 12){
console.log('The password is mighty strong');
}
else if(password.length >= 8){
console.log('The password is strong enough');
}
else{
console.log('password is not long enough');
}
//Logical operators - OR || and AND &&
const password1 = 'Rwanda'; //This password is not strong because the password is under and not equal to 12 or 8.
//And in the password there's no @ included
if(password1.length >= 12 && password1.includes('@')){
console.log('The password is mighty strong');
}
else if(password1.length >= 8 || password1.includes('@') && password1.length >=5){
console.log('The password is strong enough');
}
else{
console.log('The password is not strong');
}
//Logical NOT ( ! )
let user = false;
if(!user){
console.log('You must be logged in to continue');
}
//trick of the logical NOT ( ! )
console.log(!true); //This will turn to false because of the NOT ( ! )
console.log(!false); //This will turn to true because of the NOT ( ! )
//break and continue
const score2 = [50,25,0,30,100,20,10];
for(i = 0; i < score2.length; i++){
if(score2[i] === 0){
continue;
}
console.log('your score:', score2[i]);
if(score2[i] === 100){
console.log('congrats, you got the top score');
break;
}
}
//statements
const grade = 'C';
switch(grade){
case 'A':
console.log('You got an A!');
break;
case 'B':
console.log('You got a B!');
break;
case 'C':
console.log('You got a C!');
break;
case 'D':
console.log("You got a D!");
break;
case 'E':
console.log('You got a E!');
break;
default:
console.log('not a valid grade');
}
//function declaration
function greet(){
console.log('Hello there');
}
//function expression (calling the function)
greet();
//you can call it many times you want
greet();
greet();
greet();
//Another way of function expression ( The best way, recommanded by Ninja )
const speak = function(){
console.log('Have a good day!');
};
speak();
speak();
speak();
//Arguments & Parameters
//Example1
// const sum = funtion('a','b'){
// console.log(a + b);
// };
// sum(1, 2);
const speak1 = function(name,time){
console.log(`Good ${time} ${name}`);
};
speak1('Selmy','morning');
//another way
const speak2 = function(name = 'Nshuti', time = 'night'){
console.log(`Good ${time} ${name}`);
};
speak2();
//returning values on regular function
const calcArea = function(radius){
let area = 3.14 * radius ** 2; //Area
return area; // return the area
};
const a = calcArea(5); //And the area returns here.
console.log(a); //execute
//Another way to return the function
const calc$area = function(radius){
return 3.14 * radius ** 2;
};
const b = calc$area(5);
console.log(b);
//Arrow function //method 1
const calc$$area = (radius) =>{
return 3.14 * radius ** 2;
};
const area =calc$$area(5);
console.log('Area is :', area);
//Arrow function //method 2 // remove the brakets on the function (radius)
const calc$$$area = radius =>{
return 3.14 * radius ** 2;
};
const $area =calc$$$area(5);
console.log('Area is :', $area);
//Arrow function //method 3
const calCarea = radius => 3.14 * radius ** 2;
const Area =calCarea(5);
console.log('Area is :', Area);
//practice arrow functions for :
const greett = function(){
return 'hello, world';
};
const say = greett();
console.log(say);
// answer in row function
const $greet = () => 'hello, world';
const sayy = $greet();
console.log(sayy);
//practice 2
const bill = function(products, tax){
let total = 0;
for(let i = 0; i < products.length; i++){
total += products[i] + products[i] * tax;
}
return total;
}
console.log(bill([10,15,30], 0.2));
// For products[0] = 10: 10 + 10 * 0.2 = 10 + 2 = 12
// For products[1] = 15: 15 + 15 * 0.2 = 15 + 3 = 18
// For products[2] = 30: 30 + 30 * 0.2 = 30 + 6 = 36
//arrow function :
const bill1 = (products, tax) => {
let total = 0;
for(let i = 0; i<products.length; i++){
total+=products[i] + products[i] *tax;
}
return total;
}
console.log(bill([10,15,30],0.2));``
//example of CallBack function
//Example 1
const myFunc =(callbackFunc) =>{
//do something
let value = 50;
callbackFunc(value);
};
myFunc(function(value){
//do something
console.log(value);
});
//Example 2
// A function that simulates a network request
function fetchData(url, callback) {
setTimeout(() => {
const data = { name: "John Doe", age: 30 }; // Simulated data
callback(data); // Execute the callback with the data
}, 2000); // Simulate a 2-second network delay
}
// Usage of the fetchData function with a callback
// fetchData('https://api.example.com/user', (data) => {
// console.log('Data received:', data);
// });
// const Keys = require('./keysss');
const key = 'http://localhost:5500';
const backAPI = `${key}/employees`;
console.log("BACKEND API:", backAPI);
const five = 5;
const six = 6;
// const fuc = () => {
// try{
// }catch(error){
// console.error("Error: ", error);
// }
// }
console.log(`Multiplication of: ${five} and ${six} is ${five * six}`);
let people = ['mario', 'luigi', 'ryu', 'shaun', 'chun-li']
const logPerson1 = (person, index) => {
console.log(`${index} hello ${person}`);
};
people.forEach(logPerson1);
const logPerson = (person, index) => {
// for(i=1; i<person.length; i++){}\
const personx = person;
const indexx = index + 1;
console.log(indexx , personx);
}
people.forEach(logPerson);
//get a reference to the 'ul'
const ul = document.querySelector('.people1');
const people1 = ['mario','luigi','ryu','shaun','chun-li'];
let html = ``;
// people1.forEach(function(person){
people1.forEach(person =>{
//create html template
html += `<li style = "color: purple">${person}</li>`;
});
console.log(html);
ul.innerHTML = html;
//Object literals
let user1 = {
name : 'Nshuti',
age : 26,
email : '[email protected]',
location : 'Rwanda',
blogs : ['why mac & cheese rules', '10 things to make with marmite']
};
console.log(user1);
console.log(user1.name);
//over-write
user1.age = 30;
console.log(user1.age);
//another way to access the object
console.log(user1['email']);
//example2
user1['location'] = 'United states';
console.log(user1['location']);
//check the type by using typeof
console.log(typeof user1);
//Object literals
//method in Object
//Example1
// Define an object
let car = {
brand: 'Toyota',
model: 'Camry',
year: 2022,
// Method definition
displayInfo: function() {
return `${this.year} ${this.brand} ${this.model}`;
}
};
// Accessing the method
console.log(car.displayInfo()); // Output: 2022 Toyota Camry
//add method to object
//Example1
let user2 = {
name : 'Selmy',
age : 26,
email : '[email protected]',
location : 'United states',
login : function() {
console.log('The user logged in');5
},
logout : function() {
console.log('The user logged out');
}
};
user2.login();
user2.logout();
//example2
let user3 = {
name : 'Selmy',
age : 26,
email : '[email protected]',
location : 'United states',
blogs : ['why mac & cheese rules', '10 things to make with marmite'],
login : function() {
console.log('The user logged in');5
},
//another way to write the function
logout() {
console.log('The user logged out');
},
//how to access another object
logBlogs : function(){
// console.log(this.blogs);
console.log('This user has written the following blogs : ');
this.blogs.forEach(blog => {
console.log(blog);
})
}
};
user3.logBlogs();
//Math Object
//Examples...
console.log(Math);
console.log(Math.PI);
console.log(Math.E);
//Another example...
const area1 = 7.7;
console.log(Math.round(area1)); //the result is 8 because 7.7 is almost being 8
console.log(Math.floor(area1)); //the result is 7 because floor the number goes down
console.log(Math.ceil(area1)); //the result is 8 because the number goes up
console.log(Math.trunc(area1)); //the number is 7 because the trunc removes the decimal
//random numbers
const random = Math.random();
console.log(random);
//way to get a random number
console.log(Math.round(random *100));
//Primitive Values
let scoreOne = 50;
let scoreTwo = scoreOne;
console.log(`scoreOne: ${scoreOne}`,`scoreOne: ${scoreTwo}`);
scoreOne = 100;
console.log(`scoreOne: ${scoreOne}`,`scoreOne: ${scoreTwo}`);
const userOne = {name: 'Selmy', age: 26};
const userTwo = userOne;
console.log(userOne, userTwo);
userOne.age = 36;
console.log(userOne, userTwo);
// DOM ( Document Object Model)
const para1 = document.querySelector('p');
console.log(para1);
//access the class on p
const para2 = document.querySelector('.error');
console.log(para2);
//access another error (div error)
const para3 = document.querySelector('div.error');
console.log(para3);
//how to find and copy a selector on web
//click on the selector => right click => inspect => copy => copy selector and then copy in vscode
const para4 = document.querySelector('body > h1'); //copy selector this => (body > h1)
console.log(para4);
//what if we want to grab all tags
//for example 'P' tags
const paras = document.querySelectorAll('p');
console.log(paras);
//this is how you can select a single 'P' in querySelectAll
console.log(paras[1]);
//or you can use forEach method to select each 'P' in querySelectAll
paras.forEach(pare => {
console.log(pare);
});
//Different ways to grab elements
// 1. get an element by ID
const Tittle = document.getElementById('page-tittle');
console.log(Tittle);
// 2. get elements by their class name
const error = document.getElementsByClassName('error');
console.log(error);
//Like a node list we can still use square brackets notation to get a single element from that list or that colection
console.log(error[1]);
//But we can not use forEach method on the HTML collection
// 3. get elements by their tag name
const pares = document.getElementsByTagName('p');
console.log(pares);
//Like a node list we can still use square brackets notation to get a single element from that list or that colection
console.log(pares[2]);
//How do we actually change or add or text
// const mode = document.querySelector('p');
// // console.log(mode.innerText);
// //change text
// mode.innerText = 'Selmy is the best!';
// //add a text (we just add + to =)
// // mode.innerText += 'Selmy is the best!';
// //what if i want to change the text for severals items at once
// const mode1 = document.querySelectorAll('p');
// mode1.forEach(made => {
// console.log(made.innerText);
// made.innerText += 'New text';
// });
//what if we want to change HTML or something
const demi = document.querySelector('.content');
console.log(demi.innerHTML);
//what if we want to update it / change it
const content = document.querySelector('.content');
content.innerHTML = '<h2>THIS IS A NEW H2</h2>';
//adding a text by using plus, here's to add the arrays
//example
const people2 = ['mario', 'luigi', 'yoshi'];
people2.forEach(person =>{
content.innerHTML += `<p>${person}</p>`;
});
//access the attribute
const link = document.querySelector('a');
console.log(link.getAttribute('href'));
//what if we want to change attribute
link.setAttribute('href', 'https://www.thenetninja.co.uk');
//we can also change the innertex
link.innerText = 'The Net Ninja Website';
// // another example
// const mssg = document.querySelector('p');
// console.log(mssg.getAttribute('class'));
// //we can also change the attribute
// mssg.setAttribute('class', 'success');
// //we can aslo change style. like color
// mssg.setAttribute('style', 'color: green;');
//styling in javascript
const title = document.querySelector('h1');
//set a margin by using javascript
// title.setAttribute('style', 'margin: 50px;');
//we first console it to get css style's properties
console.log(title.style);
//if we want to check a css style
console.log(title.style.color);
//what if we want to add a style
//example 1 (change the margin)
title.style.margin = '50px';
//example 2 (change the color)
title.style.color = 'crimson';
//example 3 (change the font size)
title.style.fontSize = '60px';
// what if we want to delete a style
title.style.margin = ''; //we just empty the string
//use style in JS
const content0 = document.querySelector('p');
//the way to get classes element has
console.log(content0.classList);
//what if we want to add or remove a class
content0.classList.add('error0');
//what if we want to remove a class
content0.classList.remove('error0');
//Npow let's add a success class
content0.classList.add('success');