Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (114 loc) · 2.69 KB
/
Copy pathscript.js
File metadata and controls
171 lines (114 loc) · 2.69 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
// https://chatgpt.com/share/6ab40436-21b0-83ee-b78b-e63f2815966d
// Switch
// switch (key) {
// case value:
// break;
// default:
// break;
// }
let day = 3;
switch (day) {
case 1: // ismy condition aaegy
console.log("Monday");
break; // ismy break lagana lazmi hai qk jha sy condition true hogy wha sy neechy tak print krdega
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Friday");
break;
default:
console.log("Invalid Day"); // agr condition match na howy ye default rahega
break;
}
let fruit = "mango";
switch (fruit) {
case "apple":
console.log("Apple");
break;
case "banana":
console.log("Banana");
break;
case "mango":
console.log("Mango");
break;
default:
console.log("No Fruit Found");
break;
}
// trim()
let a = " Hashir Dilmurad Zai ";
console.log(a);
a = a.trim(); // Start and End k Spacing Khtam karega
console.log(a);
let i = 1;
// While Loop
// pehly condition check phir code chalega
// while (condition) {
// }
while (i <= 5) {
console.log(i);
i++;
}
let fruits = ["apple", "mango", "banana"];
while (fruits.length) {
console.log(fruits.pop());
}
// Do While Loop
// while ka olta pehly code chalega phir condition check
// do {
// } while (condition);
let number;
do {
number = Number(prompt("Enter a number greater than 0"));
console.log(`${number} is good number`);
alert(`${number} is good number`);
} while (number <= 0);
let anumber = 6;
do {
console.log(anumber);
} while (anumber < 5);
// for...of loop
// for...of loop ka use mostly array ke andar se direct values nikalne ke liye hota hai.
// for (const element of object) {
// }
let name = "Hashir Dilmurad Zai";
for (let char of name) {
console.log(char);
}
for (let fruitarray of fruits) {
console.log(fruitarray);
}
// for...in loop
// for (const key in object) {
// if (!Object.hasOwn(object, key)) continue;
// const element = object[key];
// }
const car = {
brand: "Toyota",
model: "Corolla",
year: 2020,
};
for (let key in car) {
console.log("value:", car[key]);
console.log("key:", key);
}
// 👉 Objects → for...in
// 👉 Arrays / strings → for...of
// | Loop | Kya deta hai? |
// | ---------- | --------------- |
// | `for...in` | **key / index** |
// | `for...of` | **value** |
// Map()
// map() function JavaScript mein arrays ke saath use hota hai. Iska main kaam hai:
const numbers = [1, 2, 3, 4];
const result = numbers.map(function (num) {
return num * 2; // map me return zarory hota wrna undefined hojaega
});
console.log(result);
fruits.map(function (a) {
console.log(a);
});