-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPracticeMore.js
More file actions
2860 lines (2282 loc) · 76.6 KB
/
PracticeMore.js
File metadata and controls
2860 lines (2282 loc) · 76.6 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
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! ===============================
// Area and perimeter
//! ===============================
//? https://youtube.com/shorts/pebzKDY8M8Q?si=oYcuRm1d9fuiKjRE
//? What is Area?
//* Definition: The space inside a shape.
//! Example: How much floor space a room has or how much land a garden covers.
// Units: Square units (e.g., m², cm²).
//? What is Perimeter?
//* Definition: The total distance around a shape.
//! Example: The length of a fence needed to surround a yard or the distance around a running track.
// Units: Regular units (e.g., meters, centimeters).
//! ============= Rectangle ==============
// Rectangle:
// Area: length * width
// Perimeter: 2 * (length + width)
function Rectangle(length, width) {
let Area = length * width;
let perimeter = 2 * length + 2 * width;
console.log(`Rectangle - Area: ${Area}, Perimeter: ${perimeter}`);
}
Rectangle(12, 10);
//? Rectangle - Area: 120, Perimeter: 44
//! ============= Square ==================
// Square:
// Area: side * side
// Perimeter: 4 * side
function Square(side) {
let Area = side * side;
let perimeter = side + side + side + side; // 4 * side
console.log(`Square - Area: ${Area}, Perimeter: ${perimeter}`);
}
Square(10);
//? Square - Area: 100, Perimeter: 40
//! ============= Circle ==================
// Circle:
// The radius of a circle is the distance from its center to any point on its edge. It is denoted as r and is used to calculate the area and circumference of the circle.
// Area: π * radius * radius or π * radius^2
// Perimeter (Circumference): 2 * π * radius
function circle(radius) {
const area = Math.PI * radius * radius; // π × r²
const perimeter = 2 * Math.PI * radius; // 2 × π × r
console.log(`Circle - Area: ${area}, Circumference: ${perimeter}`);
}
circle(7);
//? Circle - Area: 153.93804002589985, Circumference: 43.982297150257104
//! ============= Triangle ==================
// Triangle (assuming base and height):
// Area: 0.5 * base * height
// Perimeter: Sum of all three sides
function triangle(base, height, side1, side2) {
const area = 0.5 * base * height;
const perimeter = base + side1 + side2; // Sum of all sides
console.log(`Triangle - Area: ${area}, Perimeter: ${perimeter}`);
}
triangle(5, 6, 3, 4);
//? Triangle - Area: 15, Perimeter: 12
//! ================================================================================================
//! ================================================================================================
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! check the value is in between range of Two number or not
//? ----------------------------------------------
const checkNum = (StartingRange, EndingRage, InputValue) => {
if (InputValue > StartingRange && InputValue < EndingRage) {
console.log(`The value ${InputValue} falls within the specified range.`);
} else {
console.log(
`The value ${InputValue} not falls within the specified range.`
);
}
};
const StartingRange = 2;
const EndingRage = 5;
const InputValue = 3;
checkNum(StartingRange, EndingRage, InputValue);
//? The value 3 falls within the specified range.
//? ----------------------------------------------
const checkNum2 = (StartingRange, EndingRage, InputValue) => {
if (InputValue > StartingRange) {
if (InputValue < EndingRage) {
console.log(`The value ${InputValue} falls within the specified range.`);
return;
}
}
console.log(
`The value ${InputValue} does not fall within the specified range.`
);
};
checkNum2(2, 5, 3);
//? The value 3 falls within the specified range.
//? ----------------------------------------------
const checkNum3 = (StartingRange, EndingRage, InputValue) => {
if (InputValue > StartingRange) {
if (InputValue < EndingRage) {
console.log(`The value ${InputValue} falls within the specified range.`);
} else {
console.log(
`The value ${InputValue} does not fall within the specified range.`
);
}
} else {
console.log(
`The value ${InputValue} does not fall within the specified range.`
);
}
};
checkNum3(2, 5, 3);
//? The value 3 falls within the specified range.
checkNum3(2, 5, 1);
//? The value 1 does not fall within the specified range.
checkNum3(2, 5, 6);
//? The value 6 does not fall within the specified range.
//! ================================================================================================
//! ================================================================================================
//! -----------------------
//! Get the value between two number
//! -----------------------
const ValueBetweenTwoNum = (num1, num2) => {
for (let i = num1 + 1; i < num2; i++) {
console.log(i);
}
};
ValueBetweenTwoNum(2, 6);
//? 3
//? 4
//? 5
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! Sum of 2 number
//? ----------------------------------------------
const SumOfTwoNumber = (num1, num2) => {
const result = num1 + num2;
return result;
};
console.log(SumOfTwoNumber(1, 2));
//? 3
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! Multiply 2 number with operator and without operator
//? ----------------------------------------------
const multiplesNum = (num1, num2) => {
const result = num1 * num2;
return result;
};
console.log(multiplesNum(2, 3));
//? 6
//? without operator
const multiplesNum2 = (num1, num2) => {
let result = 0;
for (let i = 1; i <= num2; i++) {
result = result + num1;
}
return result;
};
console.log(multiplesNum2(3, 10));
//? 30
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! // Function to calculate base raised to the power of exponent
//? ----------------------------------------------
const calculateExponential = (base, exponent) => {
let result = 1;
for (let i = 1; i <= exponent; i++) {
result = result * base;
}
return `${base} raised to the power of ${exponent} is : ${result}`;
};
console.log(calculateExponential(2, 4));
//? 2 raised to the power of 4 is : 16
//? ------------------------
const calculateExponential2 = (base, exponent) => {
let result = base ** exponent;
return `${base} raised to the power of ${exponent} is : ${result}`;
};
console.log(calculateExponential2(2, 4));
//? 2 raised to the power of 4 is : 16
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! // Find the largest of two numbers
//? ----------------------------------------------
function findLargest(a, b) {
if (a > b) {
console.log(a + " Value a is larger");
} else if (b > a) {
console.log(b + " Value b is larger");
} else {
console.log("Both are equal");
}
}
findLargest(5, 10); //? 10 Value b is larger
findLargest(20, 10); //? 20 Value a is larger
findLargest(7, 7); //? Both are equal
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! // Check if a number is positive, negative, or zero
//? ----------------------------------------------
function checkNumber(num) {
if (num > 0) {
console.log(num + " is Positive");
} else if (num < 0) {
console.log(num + " is Negative");
} else {
console.log(num + " is Zero");
}
}
checkNumber(5); //? 5 is Positive
checkNumber(-8); //? -8 is Negative
checkNumber(0); //? 0 is Zero
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! Grade Checker
//? ----------------------------------------------
function assignGrade(marks) {
if (marks >= 90) {
console.log("Grade: A");
} else if (marks >= 75 && marks < 90) {
console.log("Grade: B");
} else if (marks >= 50) {
console.log("Grade: C");
} else {
console.log("Fail");
}
}
// Example usage
assignGrade(85); //? Output: Grade: B
//! ================================================================================================
//! ================================================================================================
//! ===============================
// leap year or not
//! ===============================
// The code checks whether a given year is a leap year or not. Here's a detailed explanation:
//* Leap Year Rule:
// A leap year is a year that:
// Is divisible by 4, but not divisible by 100, unless it is also divisible by 400.
// This means:
// Years like 2000 and 1600 are leap years because they are divisible by 400.
// Years like 1900 and 2100 are not leap years because they are divisible by 100 but not by 400.
// Years like 2020 and 2024 are leap years because they are divisible by 4 but not divisible by 100.
function leapYear(year) {
if (year % 4 == 0 && year % 100 != 0) {
return year + " is a leap year"; // Case for being divisible by 4 but not 100
} else if (year % 400 === 0) {
return year + " is a leap year"; // Case for being divisible by 400
} else {
return year + " is not a leap year"; // If it doesn't satisfy either of the above conditions
}
}
console.log(leapYear(2000)); //? "2000 is a leap year"
console.log(leapYear(1900)); //? "1900 is not a leap year"
console.log(leapYear(2024)); //? "2024 is a leap year"
console.log(leapYear(2025)); //? "2025 is not a leap year"
//? ----------------------------------------------
//? ----------------------------------------------
function leapYear2(year) {
if (year % 4 === 0) {
if (year % 100 !== 0) {
return year + " is a leap year";
} else if (year % 400 === 0) {
return year + " is a leap year";
} else {
return year + " is not a leap year";
}
} else {
return year + " is not a leap year";
}
}
console.log(leapYear2(2000)); //? "2000 is a leap year"
console.log(leapYear2(1900)); //? "1900 is not a leap year"
console.log(leapYear2(2024)); //? "2024 is a leap year"
console.log(leapYear2(2025)); //? "2025 is not a leap year"
//! ================================================================================================
//! ================================================================================================
//! ===============================
// a,b,c no of this three greater than
//! ===============================
const numA = 10;
const numB = 20;
const numC = 5;
function GreaterNum(a, b, c) {
if (a >= b && a >= c) {
console.log(` ${a} is the Greatest number`);
} else if (b >= c) {
console.log(` ${b} is the Greatest number`);
} else {
console.log(` ${c} is the Greatest number`);
}
}
GreaterNum(numA, numB, numC); //? 20 is the Greatest number
//? -----------------------------------
function GreaterNum(a, b, c) {
if (a >= b) {
if (a >= c) {
console.log(`${a} is the Greatest number`);
} else {
console.log(`${c} is the Greatest number`);
}
} else if (b >= c) {
console.log(`${b} is the Greatest number`);
} else {
console.log(`${c} is the Greatest number`);
}
}
GreaterNum(10, 20, 5); //? Output: 20 is the Greatest number
//! =============================================================================================
//! =============================================================================================
//! =============================================================================================
//? find temp in c pass in f and then check given temp is cold normal or hot
//! =============================================================================================
const temperature = 30;
const temperature2 = 60;
const temperature3 = 80;
function temp(fahrenheit) {
const celsius = (fahrenheit - 32) * (5 / 9);
if (celsius < 15) {
console.log(`${Math.floor(celsius)}°C - Cold`);
} else if (celsius <= 25) {
// No need to check celsius >= 15 because the first condition already ensures this.
console.log(`${Math.floor(celsius)}°C - normal`);
} else {
console.log(`${Math.floor(celsius)}°C - hot`);
}
}
temp(temperature); //? -1.11°C - Cold
temp(temperature2); //? 15.56°C - normal
temp(temperature3); //? 26.67°C - hot
//! =============================================================================================
//! =============================================================================================
//! ===============================
//? find the greatest number among four numbers:
//! ===============================
function GreaterNum(a, b, c, d) {
if (a >= b && a >= c && a >= d) {
console.log(`${a} is the Greatest number`);
} else if (b >= c && b >= d) {
console.log(`${b} is the Greatest number`);
} else if (c >= d) {
console.log(`${c} is the Greatest number`);
} else {
console.log(`${d} is the Greatest number`);
}
}
GreaterNum(10, 20, 5, 30); //? Output: 30 is the Greatest number
GreaterNum(50, 40, 60, 30); //? Output: 60 is the Greatest number
//? -----------------------------------
function GreaterNum(a, b, c, d) {
if (a >= b) {
if (a >= c) {
if (a >= d) {
console.log(`${a} is the Greatest number`);
} else {
console.log(`${d} is the Greatest number`);
}
} else if (c >= d) {
console.log(`${c} is the Greatest number`);
} else {
console.log(`${d} is the Greatest number`);
}
} else if (b >= c) {
if (b >= d) {
console.log(`${b} is the Greatest number`);
} else {
console.log(`${d} is the Greatest number`);
}
} else if (c >= d) {
console.log(`${c} is the Greatest number`);
} else {
console.log(`${d} is the Greatest number`);
}
}
GreaterNum(10, 20, 5, 30); //? Output: 30 is the Greatest number
GreaterNum(50, 40, 60, 30); //? Output: 60 is the Greatest number
//? -----------------------------------
function GreaterNum(a, b, c, d) {
// Compare all the numbers and find the greatest
let largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
if (d > largest) {
largest = d;
}
console.log(largest + " is the greatest number");
}
GreaterNum(10, 20, 5, 30); //? Output: 30 is the greatest number
//! ================================================================================================
//! ================================================================================================
//! ===============================
//? Check if a person is eligible to vote
//! ===============================
function canVote(age) {
if (age >= 18) {
console.log("Eligible to vote");
} else {
console.log("Not eligible to vote");
}
}
canVote(20); //? Eligible to vote
canVote(15); //? Not eligible to vote
//! ================================================================================================
//! ================================================================================================
//! -----------------------
//! Print 1,2,3,4,5,6,7,8,...n
//! -----------------------
function series1(num) {
for (let i = 1; i <= num; i++) {
console.log(i);
}
}
series1(5);
// 1
// 2
// 3
// 4
// 5
//! -------------------- OR ----------------------
function series11(num) {
let result = "";
for (let i = 1; i <= num; i++) {
result += i + " "; // Concatenate each number to the result string
}
console.log(result); // Print the result string without trailing space
}
series11(5);
// 1 2 3 4 5
//! ================================================================================================
//! ================================================================================================
//! -----------------------
//! Print 1,-2,3,-4,5,-6,7,-8,...n
//! -----------------------
function series2(num) {
for (let i = 1; i <= num; i++) {
if (i % 2 == 0) {
console.log(`-${i}`);
} else {
console.log(`${i}`);
}
}
}
series2(5);
// 1
// -2
// 3
// -4
// 5
//! -------------------- OR ----------------------
function series22(num) {
let sign = 1;
for (let i = 1; i <= num; i++) {
let value = i * sign;
console.log(value);
sign = -sign; // Flip the sign for next number
}
}
series22(5);
// 1
// -2
// 3
// -4
// 5
//! ================================================================================================
//! ================================================================================================
//! ===============================
// 1,4,9,16,25,......,N
//! ===============================
function series3(num) {
for (let i = 1; i <= num; i++) {
let square = i * i;
console.log(`${i} * ${i} = ${square}`);
}
}
series3(5);
// 1 * 1 = 1
// 2 * 2 = 4
// 3 * 3 = 9
// 4 * 4 = 16
// 5 * 5 = 25
//! ================================================================================================
//! ================================================================================================
//! ===============================
//! 1,8,27,64,125,......,N
//! ===============================
function series4(num) {
for (let i = 1; i <= num; i++) {
let cube = i * i * i; // or use i ** 3
console.log(`${i} * ${i} * ${i} = ${cube}`);
}
}
series4(5);
// 1 * 1 * 1 = 1
// 2 * 2 * 2 = 8
// 3 * 3 * 3 = 27
// 4 * 4 * 4 = 64
// 5 * 5 * 5 = 125
//! ================================================================================================
//! ================================================================================================
//! ===============================
//! 2, 12 ,36,80,.....,N
//! ===============================
// raised to the power of two" and "raised to the power of three ( logic )
function SeriesPart3(num) {
for (let i = 1; i <= num; i++) {
let square = i * i; // Square calculation
let cube = i * i * i; // Cube calculation
let result = square + cube;
console.log(`${i}² + ${i}³ = ${square} + ${cube} = ${result}`);
}
}
SeriesPart3(10);
// 1² + 1³ = 1 + 1 = 2
// 2² + 2³ = 4 + 8 = 12
// 3² + 3³ = 9 + 27 = 36
// 4² + 4³ = 16 + 64 = 80
// 5² + 5³ = 25 + 125 = 150
// 6² + 6³ = 36 + 216 = 252
// 7² + 7³ = 49 + 343 = 392
// 8² + 8³ = 64 + 512 = 576
// 9² + 9³ = 81 + 729 = 810
// 10² + 10³ = 100 + 1000 = 1100
//? -----------------------------------
function SeriesPart3(num) {
for (let i = 1; i <= num; i++) {
let sq = i * i; // Square calculation
let cb = i * i * i; // Cube calculation
console.log(sq + cb);
}
}
SeriesPart3(10);
//? -----------------------------------
// Function to calculate square
function square(n) {
return n * n;
}
// Function to calculate cube
function cube(n) {
return n * n * n;
}
// Function to generate the series
function SeriesPart3(num) {
for (let i = 1; i <= num; i++) {
console.log(square(i) + cube(i));
}
}
SeriesPart3(10);
//! ================================================================================================
//! ================================================================================================
//! ===============================
// 0, 1, 1, 2, 3, 5, 8, 13, 21, ... (Fibonacci series)
//! ===============================
// * The Fibonacci series is a special sequence of numbers where:
// * It starts with 0 and 1.
// * Every number after that is the sum of the two numbers before it.
//? Example:
// Start with: 0, 1.
// Add the last two numbers to get the next one:
// 0 + 1 = 1, so the sequence becomes: 0, 1, 1.
// 1 + 1 = 2, so the sequence becomes: 0, 1, 1, 2.
// 1 + 2 = 3, so the sequence becomes: 0, 1, 1, 2, 3.
// 2 + 3 = 5, so the sequence becomes: 0, 1, 1, 2, 3, 5.
function Fibonacci(num) {
let a = 0;
let b = 1;
let next = 0;
let result = "";
for (let i = 1; i <= num; i++) {
result += a + " "; // Concatenating the result
next = a + b; // Updating the existing variable
a = b;
b = next;
}
return result;
}
console.log(Fibonacci(10));
// 1 1 2 3 5 8 13 21 34 55
//! ================================================================================================
//! ================================================================================================
//! ===============================
// 1! 2! 3! 4! 5! 6! .....n (1 ,2,6,24,...n) (factorial)
//! ===============================
// Factorial is a mathematical operation represented by the symbol !.
// It involves multiplying a number by all the whole numbers less than it down to 1.
// Formula:
// For any positive integer n, the factorial of n (denoted as n!) is:
// n! = n × (n - 1) × (n - 2) × ... × 1
// Example:
// 5! = 5 × 4 × 3 × 2 × 1 = 120
// 4! = 4 × 3 × 2 × 1 = 24
// 3! = 3 × 2 × 1 = 6
// 2! = 2 × 1 = 2
// 1! = 1
// Special Case:
// 0! = 1 (By definition, the factorial of zero is defined as 1).
//! ============ FOR FIND Single FACTORIAL AND GET ITS VALUE ===================
function factorial(num) {
let result = 1;
if (num < 0) {
return "Factorial is not defined for negative numbers";
} else if (num === 0) {
return 1; // 0! is defined as 1
}
for (let i = 1; i <= num; i++) {
result = result * i;
}
return result;
}
const factNum = 10;
console.log(`The factorial of ${factNum} is: ${factorial(factNum)}`);
// The factorial of 10 is: 3628800
//! ============ FOR PRINT SERIES OF FACTORIAL ===================
function factorial2(num) {
let result = 1;
for (let i = 1; i <= num; i++) {
result = result * i; // first multiply
console.log(result); // then log
}
}
const factNum2 = 5;
factorial2(factNum2);
// Output:
// 1
// 2
// 6
// 24
// 120
// IN SHORT LAST RESULT * CURRENT ITERATION MEANS 5! = 5 * 4!(24)
//! The expression result = result * i multiplies the current value of result by i in each iteration of the loop.
//? Initially: result = 1 (since we start with 1).
//? First iteration (i = 1): result = (result)1 * (i)1 → result = 1.
//? Second iteration (i = 2): result = (result)1 * (i)2 → result = 2.
//? Third iteration (i = 3): result = (result)2 * (i)3 → result = 6.
// And so on, until i reaches the number num.
//! ============ FOR PRINT SERIES OF FACTORIAL with helper loop ===================
function factorial2(num) {
let result = 1;
for (let i = 1; i <= num; i++) {
result = result * i;
}
return result;
}
const factNum234 = 5;
// Call the function for each step
for (let i = 1; i <= factNum234; i++) {
console.log(factorial2(i));
}
// 1
// 2
// 6
// 24
// 120
//! ================================================================================================
//! ================================================================================================
//! ===============================
//? 1! 3! 5! 7! .....n (1 ,6, 120...n) (factorial)
//! ===============================
function factorialOfOddNumbers(num) {
let result = 1;
for (let i = 1; i <= num; i++) {
if (i % 2 != 0) {
result = result * i;
console.log(result);
} else {
result = result * i;
}
}
return result;
}
factorialOfOddNumbers(5);
// 1
// 6
// 120
function factorialOfOddNumbers(num) {
//? same like above code but just better way
let result = 1;
for (let i = 1; i <= num; i++) {
result *= i;
if (i % 2 != 0) {
console.log(result); // only print at odd steps
}
}
return result;
}
factorialOfOddNumbers(5);
//? ---------------
//? above Code is good but alternative way
//? ---------------
function factorialOfOddNumbers2(num) {
let result = 1;
for (let i = 1; i <= num; i += 2) {
if (i === 1) {
result = result * i;
} else {
result = result * i * (i - 1);
}
console.log(`The factorial of odd number ${i} is: ${result}`);
}
return result;
}
factorialOfOddNumbers2(5);
// The factorial of odd number 1 is: 1
// The factorial of odd number 3 is: 6
// The factorial of odd number 5 is: 120
//? ---------------
//? above Code is good but alternative way
//? ---------------
function oddFactorials(n) {
for (let i = 1; i <= n; i += 2) {
// Loop through odd numbers only
let fact = 1;
// Calculate factorial using a second for loop
for (let j = 1; j <= i; j++) {
fact = fact * j;
}
console.log(i + "! =", fact); // Print the factorial of each odd number
}
}
// Call the function
oddFactorials(7);
//? 1! = 1
//? 3! = 6
//? 5! = 120
//? 7! = 5040
//! ================================================================================================
//! ================================================================================================
//! ===============================
//! Different Between '==' and '==='
//! ===============================
var x = 7;
var y = "7";
// console.log(x == y); // true
// console.log(x === y); // false
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! // Odd or even
//? ----------------------------------------------
function checkEvenOdd(num) {
if (num % 2 === 0) {
console.log(num + " is Even");
} else {
console.log(num + " is Odd");
}
}
checkEvenOdd(7); // 7 is Odd
checkEvenOdd(10); // 10 is Even
//! ================================================================================================
//! ================================================================================================
//? ----------------------------------------------
//! // “Find the sum of all the multiples of 3 or 5 below a given number (like 10).”
//? ----------------------------------------------
function sumOfMultiples(limit) {
let sum = 0;
for (let i = 1; i < limit; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
return sum;
}
const re = sumOfMultiples(10);
console.log("Sum of multiples of 3 or 5 below 10 is:", re);
// Sum of multiples of 3 or 5 below 10 is: 23 ( 3 + 5 + 6 + 9 = 23)
//! ================================================================================================
//! ================================================================================================
//! ===============================
//! Prime Number or Not
//! ===============================
// A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself.
const PrimeOrNot = (num) => {
if (num <= 1) {
return `The Number: ${num} is not a prime number`;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return `The Number: ${num} is not a prime number`;
}
}
return `The Number: ${num} is a prime number`;
};
console.log(PrimeOrNot(5)); // The Number: 5 is a prime number
// For num = 7, the loop checks if 7 is divisible by 2, finds that it’s not, and ends the process as 7 is a prime number.
// For num = 10, it finds that 10 is divisible by 2 and immediately concludes that it’s not a prime number.
//? ---------------
const isPrime = (num) => {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
};
const printPrimeSeries = (n) => {
console.log(`Prime numbers up to ${n}:`);
for (let i = 2; i <= n; i++) {
if (isPrime(i)) {
console.log(i);
}
}
};
// Call function
printPrimeSeries(20);
// Prime numbers up to 20:
// 2