-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_loops.js
324 lines (263 loc) · 11.7 KB
/
4_loops.js
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
//* ===============================
//* Conditional statement Section
//* ===============================
//* ===============================
//* If Statement
//* ===============================
//? If Else: The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed.
//? Syntax
// if (condition) {
// // Code to be executed if the condition is true
// } else {
// // Code to be executed if the condition is false
// }
//? We can also use an else if clause to check additional conditions:
//* ===============================
//* Interview Question
//* ===============================
//! Requirements:
// If the person is 18 years or older, a citizen, and registered to vote, display a message saying they are eligible to vote.
// If the person is younger than 18, not a citizen, or not registered to vote, display a message saying they are not eligible to vote.
// If the person is 18 or older but not a citizen, display a message saying they are not eligible due to citizenship status.
// If the person is 18 or older, a citizen, but not registered to vote, display a message saying they are not eligible due to registration status.
// Extended voting eligibility checker with additional conditions
// Assume the user's age, citizenship status, and registration status as inputs
// let userAge = 12;
// let isCitizen = true; // Assume true for citizen, false for non-citizen
// let isRegistered = true; // Assume false for not registered, true for registered
// //! Check eligibility using if...else statements with multiple conditions
// if(userAge>=18){
// if(isCitizen){
// if(isRegistered){
// console.log("you are eligible to vote");
// }
// else{
// console.log("You are not eligible due to registered status");
// }
// }
// else{
// console.log("You are not eligible due to citienship status");
// }
// }
// else{
// console.log("you are not eligible");
// }
//* ===============================
//* Interview Questions
//* ===============================
//! 1: Write a program to check if a number is even or odd.
// var num= "9";
// if(num%2===0){
// console.log("number is even");
// }
// else{
// console.log("number is odd");
// }
//! 2: Write a program to check if a number is prime.
//todo Prime numbers are numbers that have only 2 factors: 1 and themselves.
// All prime numbers greater than 2 are odd.
// However, not all odd numbers are prime.
// var num = 7;
// var isPrime=true;
// for(var i=2; i<num; i++){
// if(num%i===0){
// isPrime=false;
// break;
// }
// }
// if(isPrime)
// {
// console.log("number is prime");
// }else{
// console.log("Number is not prime");
// }
//! 3: Write a program to check if a number is positive, negative, or zero.
// var num = 0;
// if(num>0){
// console.log("it is positive number");
// }
// else if(num<0){
// console.log("it is negative number");
// }
// else{
// console.log("it is zero");
// }
//* ===============================
//* Switch Statement
//* ===============================
//? Switch Statement: The switch statement is used to perform different actions based on different conditions.
//? Syntax:
// switch (expression) {
// case value1:
// // Code to be executed if expression === value1
// break;
// case value2:
// // Code to be executed if expression === value2
// break;
// // More cases can be added as needed
// default:
// // Code to be executed if none of the cases match
// }
//todo let's see the example
//! Explain how the switch statement works and what will be the output when the variable day is set to different values.
// var day = "Monday";
// switch(day){
// case "Monday":
// console.log("today is monday");
// break;
// case "Friday":
// console.log("omg lets have party today");
// break;
// case "Sunday":
// console.log("Lets go to movie");
// break;
// default:
// console.log("no condition match");
// }
//? =========================
// ? Challenge time
//? =========================
//! Write a JavaScript switch statement that takes a variable areaOfShapes representing different shapes, and based on its value, calculates and logs the area of the corresponding shape. Consider three shapes: 'Rectangle,' 'Circle,' and 'Square.' For 'Rectangle,' use variables a and b as the sides; for 'Circle,' use a variable r as the radius; and for 'Square,' use variable a as the side length. If the provided shape is not recognized, log a message saying, 'Sorry the shape is not available.' Test your switch statement with areaOfShapes set to 'Square' and sides a and b set to 5 and 10, respectively. Ensure that the correct area (25 in this case) is logged to the console.
// let areaOfShapes = "pentagon8";
// var a=5;
// var b=10;
// var result;
// switch(areaOfShapes){
// case "square":
// result = a*a;
// console.log(result);
// break;
// case "rectangle":
// result = a*b;
// console.log(result);
// break;
// case "circle":
// var r=2;
// result = 3.142 * (r*r);
// console.log(result);
// break;
// default:
// console.log("No shape matches");
// }
//! Question: Explain the purpose of the code. What is it calculating based on the values of areaOfShapes, a, and b?
//? The code calculates and logs the area of different shapes (rectangle, circle, square) based on the value of the areaOfShapes variable.
//! Question: What will be the output if areaOfShapes is set to "Square" and why?
//? The output will be the square of the variable a (25) since the case matches "Square."
//! Question: Why is there a break statement after each case in the switch statement?
//? The break statement is used to exit the switch statement after the corresponding case is executed, preventing fall-through to subsequent cases.
//! Question: If areaOfShapes is set to "Circle," what will be logged to the console, and why is the variable r defined within the case block?
//? The output will be the area of a circle with radius r (28.26) since the case matches "Circle," and r is defined within the case block.
//! Question: What will happen if areaOfShapes is set to a shape that is not covered by any of the existing case statements?
//? The default case logs "Sorry, the shape is not available" if areaOfShapes is set to a shape not covered by any existing case.
//! Question: How does the switch statement handle the flow of control based on the value of areaOfShapes?
//? The switch statement evaluates the value of areaOfShapes and executes the code block corresponding to the matching case. The break statements ensure that only the relevant code block is executed.
//* ===============================
//* While Loop
//* ===============================
// ? While Loop: A while loop in JavaScript is a control structure that repeatedly executes a block of code as long as a specified condition remains true. The loop continues iterating while the condition is true, and it terminates when the condition becomes false.
// while (condition) {
// // Code to be executed as long as the condition is true
// }
//* Simple while loop to count from 1 to 10 🧑💻
// var num = 1;
// while(num<=10){
// console.log(num);
// num++;
// }
//! practice 🧑💻
//? let's create a table of 5
// var num = 1;
// while(num<=10){
// console.log('5 * ' + num + ' = '+ 5*num);
// console.log(`5 * ${num} = ${5 * num}`);
// num++;
// }
//* ===============================
//* Do-While Loop
//* ===============================
//? Do...While Loop: A do...while loop in JavaScript is similar to a while loop, but it guarantees that the loop body will be executed at least once before checking the loop condition. The loop continues to execute while the specified condition is true, and it terminates when the condition becomes false.
// Syntax: do {
// // Code to be executed at least once
// } while (condition);
//* Simple do...while loop to count from 1 to 10 🧑💻
// var num = 1;
// do{
// console.log(num);
// num++;
// }while(num<=10)
//? Common Use Cases:
// When you want to guarantee the execution of the loop body at least once.
// When the number of iterations is not known beforehand, and you want to validate the condition after the first iteration.
//? Example: Validating User Input with a Do...While Loop(user need to write a valid number) 🧑💻
// let userInput;
// let positiveNumber;
// do{
// userInput = prompt('enter any positive number');
// positiveNumber = parseFloat(userInput);
// }while(isNaN(positiveNumber) || positiveNumber<0);
// console.log("you entered a valid positive number: ", positiveNumber);
//* ===============================
//* For Loop
//* ===============================
//? For Loop: A for loop in JavaScript is a control flow statement that allows you to repeatedly execute a block of code a specified number of times. It's particularly useful when you know the exact number of iterations needed.
// example: for (initialization; condition; iteration) {
// // Code to be executed in each iteration
// }
// Initialization: Executed before the loop starts. Often used to initialize a counter variable.
// Condition: Evaluated before each iteration. If false, the loop terminates.
// Iteration: Executed after each iteration. Typically used to update the loop control variable.
//* Simple for loop to count from 1 to 10
// for(var num = 1; num<=10; num++){
// console.log(num);
// }
//? Key Point:
// The initialization, condition, and iteration expressions are optional. You can omit any or all of them, but you must include the semicolons.
//* The code for (;;) {} represents an infinite loop in JavaScript. This construct is commonly used when you want a loop to run indefinitely or until a break statement is encountered within the loop. It's equivalent to while (true) {}.
//* use case: Game Development:
//? In game development, an infinite loop can be used to continuously update and render game frames until a specific condition (e.g., game over) is met.
// for (;;) {
// // Update game logic and render frames
// }
//? Common Use Cases:
// When you know the exact number of iterations needed.
// Iterating over elements in an array.
// Performing a task a specific number of times.
//! practice :
//! Calculate the sum of numbers from 1 to 10 using a for loop 🧑💻
// var sum=0;
// for(var num=1; num <=10; num++){
// var sum=sum+num;
// }
// console.log(sum);
//! Generating a Times Table:🧑💻
//! Example 3: Generating a times table of 5 with a for loop.
// for(var num=1; num <=10; num++){
// console.log('5 * ' + num + ' = '+ 5*num);
// }
//! Homework ➡️ JavaScript program to print table for given number (8,9,12,15) using for Loop?
//? More Practice
//!1: program To check if a year is a leap year🧑💻
//If a year is divisible by 4 and not divisible by 100, or
//If a year is divisible by 400;
//then it is a leap year. otherwise, it is not a leap year.
// var year=2028;
// if( (year % 4===0 && year % 100 != 0) || year%400 ===0 ){
// console.log("it is a leap year");
// }else{
// console.log("it is not a leap year");
// }
//! 2: Drawing Patterns with Asterisks: 🧑💻
//* i\j 1 2 3 4 5
//* ----------------------------
//* 1 * - - - -
//* 2 * * - - -
//* 3 * * * - -
//* 4 * * * * -
//* 5 * * * * *
// for (var i = 1; i <= 5; i++) {
// var pattern = "";
// for (var j = 1; j <= i; j++) {
// pattern = pattern + " *";
// }
// console.log(pattern);
// }