File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Write a program to calculate the sum of the first 10 natural numbers.
2
+
3
+ let sum = 0 ;
4
+ for ( let i = 1 ; i <= 10 ; i ++ ) {
5
+ sum += i ;
6
+ }
7
+ console . log ( sum ) ;
8
+ // Alternatively we can use sum of n natural number => n*(n+1)/2
9
+ let n = 10 ;
10
+ let naturalnumber = n * ( n + 1 ) / 2 ;
11
+ console . log ( naturalnumber ) ;
12
+
13
+ // Write a program to find the factorial of a number.
14
+
15
+ let num = 5 ;
16
+ let factorial = 1 ;
17
+ for ( let i = num ; i >= 1 ; i -- ) { //Oh yeah c'mon let's Goooo!
18
+ factorial *= i ;
19
+ }
20
+ console . log ( factorial )
21
+
22
+ // Write a program to reverse a string.
23
+
24
+ let thor = "Loki" ;
25
+ let rev = "" ;
26
+ for ( let i = thor . length - 1 ; i >= 0 ; i -- ) {
27
+ rev += thor [ i ] ; // Here we using thor[i] to locate the index value and print
28
+ }
29
+ console . log ( rev ) ;
30
+ //The coolest way to reverese in js using below code !
31
+ console . log ( thor . split ( "" ) . reverse ( ) . join ( "" ) ) ;
You can’t perform that action at this time.
0 commit comments