Skip to content

Commit 0dd1bdd

Browse files
authored
Add files via upload
1 parent cd025ec commit 0dd1bdd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

JSprogram/Day3.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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(""));

0 commit comments

Comments
 (0)