Skip to content

Commit 255ea2d

Browse files
committed
day 3 js Loops & Strings
1 parent 328b1c2 commit 255ea2d

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

Chapter 3 Loops & String.docx

519 KB
Binary file not shown.

Loops/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
<script src="scripts.js"></script>
12+
</html>

Loops/scripts.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//Loops for loop
2+
3+
for(let i=1;i<=5;i++){
4+
console.log("Hello world");
5+
}
6+
7+
//print 1 to 5
8+
9+
for(let i=1;i<=5;i++){
10+
console.log(i);
11+
}
12+
13+
//calculate sum of 1 to n
14+
let sum=0;
15+
let n=5;
16+
for(let i=1;i<=n;i++){
17+
sum=sum+i;
18+
}
19+
console.log("Sum of n numbers =",sum);
20+
21+
//while loop
22+
let i=1;
23+
while(i<=5){
24+
console.log("i=",i);
25+
i++;
26+
}
27+
28+
//do while 1 to 5
29+
let c=1;
30+
do{
31+
console.log("c=",c);
32+
c++;
33+
} while(c<=5);
34+
35+
//for of loop
36+
37+
let str="java";
38+
let size=0;
39+
for(let i of str){
40+
console.log("i=",i);
41+
size++
42+
}
43+
console.log("length of a string",size);
44+
45+
//for in loop used in accessing the object
46+
let student={
47+
name: "Nilaj",
48+
cgpa:9.34,
49+
age:26,
50+
ispass: true,
51+
};
52+
for(let key in student){
53+
console.log("key=",key,"value =",student[key]);
54+
}
55+
56+
//Q1 print all even numbers 0 to 100
57+
58+
num=100;
59+
for(let i=0;i<=num;i++){
60+
if(i%2==0){
61+
console.log("i=",i)
62+
}
63+
}
64+
// print all odd numbers 0 to 5
65+
num=5;
66+
for(let i=0;i<=num;i++){
67+
if(i%2!==0){
68+
console.log("i=",i)
69+
}
70+
}
71+
72+
73+
//Q2
74+
let gnum=25;
75+
let usernum=alert("Guess the game number");
76+
while(usernum!=gnum){
77+
usernum=prompt("You enter Wrong number.Guess Again");
78+
}
79+
console.log("Congratulation You Enterted Correct number");

String/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
<script src="scripts.js"></script>
12+
</html>

String/scripts.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//String is a sequence of characters used to represents text
2+
3+
let str="Java";
4+
console.log("String is=",str);
5+
let len=str.length
6+
console.log("Length of String=",len);
7+
//print the 0th index
8+
console.log(str[0]);
9+
10+
let obj={
11+
item:"pen",
12+
price:10,
13+
};
14+
let output=`the cost of ${obj.item} is ${obj.price} rupees`;
15+
console.log(output);
16+
17+
//Template Literals
18+
let specialstring=`This is a template literals`;
19+
console.log(specialstring);
20+
console.log(typeof specialstring);
21+
22+
//uppercase lower case
23+
let str1="DeVOps";
24+
let str2=str1.toUpperCase();
25+
console.log("Upercase =",str2);
26+
let str3=str1.toLowerCase();
27+
console.log("Lowercase=",str3);
28+
29+
//trim removes white spaces
30+
let abc=" Java ";
31+
console.log(abc.trim());
32+
33+
//slice
34+
let bcd="hello";
35+
console.log(bcd.slice(0,3));
36+
37+
//concat
38+
let a="Rohit";
39+
let b=" Sharma";
40+
let res=a.concat(b);
41+
let c= a+b;
42+
console.log(res);
43+
console.log(c);
44+
45+
46+
//replace
47+
let bbc="hello";
48+
console.log(bbc.replace("h","m"));
49+
50+
//char at
51+
let acd="ilovejs";
52+
console.log(acd.charAt(0));
53+
54+
55+
//Q1
56+
let fullname=prompt("Enter your Fullname");
57+
let username="@"+fullname+fullname.length;
58+
console.log(username);

0 commit comments

Comments
 (0)