Skip to content

Commit ffb866e

Browse files
authored
Add files via upload
1 parent f8fe71d commit ffb866e

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Q1) Write A Program To Print Marks Of Student In An Object Using For Loop
2+
const marks = {
3+
dp: 100,
4+
ap: 99,
5+
hp: 98
6+
};
7+
8+
for (let student in marks) {
9+
console.log(`${student}: ${marks[student]}`);
10+
}
11+
12+
// Output
13+
// dp: 100
14+
// ap: 99
15+
// hp: 98
16+
17+
// Q2) Write A Program In Q1 Using Using For In Loop
18+
const marks = {
19+
dp: 100,
20+
ap: 99,
21+
hp: 98
22+
};
23+
24+
for (let student in marks) {
25+
console.log(`${student}: ${marks[student]}`);
26+
}
27+
28+
// Output
29+
// dp: 100
30+
// ap: 99
31+
// hp: 98
32+
33+
34+
// Q3) Write A Program To Print "Try Again" Until The User Enter The Correct Number
35+
36+
let correctNumber = 7;
37+
let userNumber;
38+
39+
do {
40+
userNumber = parseInt(prompt('Enter a number:'));
41+
if (userNumber !== correctNumber) {
42+
console.log('Try again');
43+
}
44+
} while (userNumber !== correctNumber);
45+
46+
// This program will keep prompting the user to enter a number until they enter the correct number (in this case, 7).
47+
48+
// Q4) Write A Function To Find the Mean Of 5 Numbers
49+
50+
function findMean(num1, num2, num3, num4, num5) {
51+
const sum = num1 + num2 + num3 + num4 + num5;
52+
const mean = sum / 5;
53+
return mean;
54+
}
55+
56+
const result = findMean(10, 20, 30, 40, 50);
57+
console.log('Mean:', result);
58+
59+
// Output
60+
61+
// Mean: 30
62+
63+
// Q5)Write a function named multiplyByTwo that takes a number as an argument and returns the result of multiplying that number by 2.
64+
65+
function multiplyByTwo(num) {
66+
return num * 2;
67+
}
68+
69+
// Q6)Write a function named reverseString that takes a string as an argument and returns the reverse of that string.
70+
71+
function reverseString(str) {
72+
return str.split('').reverse().join('');
73+
}
74+
75+
// Q7)Write a function named printEvenNumbers that takes a number as an argument and prints all the even numbers from 0 to that number.
76+
77+
function printEvenNumbers(num) {
78+
for (let i = 0; i <= num; i++) {
79+
if (i % 2 === 0) {
80+
console.log(i);
81+
}
82+
}
83+
}
84+
85+
// Q8) Write a function named calculateFactorial that takes a number as an argument and returns the factorial of that number.
86+
87+
function calculateFactorial(num) {
88+
if (num === 0 || num === 1) {
89+
return 1;
90+
}
91+
92+
let factorial = 1;
93+
for (let i = 2; i <= num; i++) {
94+
factorial *= i;
95+
}
96+
97+
return factorial;
98+
}
99+
100+
// Q9) Write a function named countOccurrences that takes an array of numbers and a target number as arguments and returns the number of times the target number appears in the array.
101+
102+
function countOccurrences(arr, target) {
103+
let count = 0;
104+
for (let num of arr) {
105+
if (num === target) {
106+
count++;
107+
}
108+
}
109+
return count;
110+
}
111+
112+
// Q10) What is an arrow function in JavaScript?
113+
114+
// An arrow function is a shorter syntax for writing function expressions. It uses the => arrow token and does not bind its own this value. It is often used for writing concise and inline functions.

Ch 3 Loops & Functions.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Loops & Functions
2+
// Loops and functions are essential concepts in JavaScript programming that help to create efficient and reusable code.
3+
4+
5+
// Types Of Loops
6+
//For Loop -> Loop a Block Of Code No Of Time
7+
//For In Loop -> Loop Through The Key Of An Object
8+
//For Of Loop -> Loop Through The Value Of An Object
9+
//While Loop -> Loop Of Block Based On Specific Condition
10+
// Do...While Loop -> While Loop Variant Which Run a At Least Once
11+
12+
13+
// For Loop
14+
// Syntax
15+
// for(Statement1;Statement2;Statement3){
16+
// // Code To Be Executed
17+
// }
18+
// Statement1 -> Executed One Time
19+
// Statement2 -> Condition Based -> Based On This Loop Body Will Be Executed
20+
// Statement3 -> Executed Every Time The Loop Body Is Executed
21+
22+
23+
// Example
24+
for (let i = 0; i < 5; i++) {
25+
console.log(i); // prints numbers from 0 to 4
26+
}
27+
28+
29+
// For In Loop
30+
//Loop Through The Key Of An Object
31+
// For In Loop Works With Array Also
32+
33+
// Example
34+
const person = {
35+
name: 'John',
36+
age: 30,
37+
occupation: 'Developer'
38+
// key:'value'
39+
};
40+
41+
for (let key in person) { //(let a in person) -> console.log(a);
42+
console.log(key); // prints "name", "age", "occupation"
43+
console.log(person[key]); // prints the corresponding values "John", 30, "Developer"
44+
}
45+
46+
47+
// For Of Loop
48+
// Loop Through The Value Of An Object
49+
// Object Must Be Iterable
50+
51+
// Example
52+
const fruits = ['apple', 'banana', 'orange'];
53+
54+
for (let fruit of fruits) {
55+
console.log(fruit); // prints "apple", "banana", "orange"
56+
}
57+
58+
const message = 'Hello';
59+
60+
for (let char of message) {
61+
console.log(char); // prints "H", "e", "l", "l", "o"
62+
}
63+
64+
65+
// While Loop
66+
// If Condition Never False -> Loop Will Never End -> Crush JS Run Time
67+
// Also Called In Infinite Loop -> Don't Try This Circus
68+
69+
// Syntax
70+
// while(condition){
71+
// // Code To Be Executed
72+
// }
73+
74+
// Example
75+
let i = 0;
76+
while (i < 5) {
77+
console.log(i); // prints numbers from 0 to 4
78+
i++;
79+
}
80+
81+
82+
// ALT + SHIFT + DOWN ARROW KEY -> Replicate Selected Code
83+
84+
85+
// Do.....While Loop
86+
// Do...While Loop -> While Loop Variant Which Run a At Least Once
87+
// Syntax
88+
89+
// do {
90+
// // Code To Be Executed
91+
// } while(Condition)
92+
93+
// Example
94+
let i = 0;
95+
do {
96+
console.log(i); // prints numbers from 0 to 4 -> Executed At Least Once
97+
i++;
98+
} while (i < 5);
99+
100+
let i = 6;
101+
do {
102+
console.log(i); // prints numbers from 0 to 4 -> Executed At Least Once
103+
i++;
104+
} while (i < 5);
105+
106+
// Output -> 6
107+
108+
109+
// Functions
110+
// Functions are reusable blocks of code that perform a specific task. They help in organizing code, improving reusability, and reducing redundancy. A function can accept parameters (inputs) and return a value.
111+
112+
// function myfun(parameter1, parameter2){
113+
// //Code -> Parameter Behave As Local Variables
114+
// }
115+
116+
// myfun(6,7); -> Function Invocation
117+
// A. Function declaration:
118+
// B function declaration defines a named function that can be called later in the code.
119+
120+
// Example
121+
function greet(name) {
122+
console.log(`Hello, ${name}!`);
123+
}
124+
125+
greet("John"); // prints "Hello, John!"
126+
greet("Sarah"); // prints "Hello, Sarah!"
127+
128+
// Function expression:
129+
// A function expression assigns a function to a variable. It can be anonymous or named.
130+
131+
const greet = function (name) {
132+
console.log(`Hello, ${name}!`);
133+
};
134+
135+
greet("John"); // prints "Hello, John!"
136+
greet("Sarah"); // prints "Hello, Sarah!"
137+
138+
// Arrow function:
139+
// Arrow functions provide a concise syntax for writing functions. They are anonymous and lexically bind the this value.
140+
141+
const greet = (name) => {
142+
console.log(`Hello, ${name}!`);
143+
};
144+
145+
greet("John"); // prints "Hello, John!"
146+
greet("Sarah"); // prints "Hello, Sarah!"
147+
148+
// Returning a value:
149+
// Functions can return a value using the return statement. The returned value can be stored in a variable or used directly.
150+
151+
function add(a, b) {
152+
return a + b;
153+
}
154+
155+
const result = add(3, 4);
156+
console.log(result); // prints 7
157+
158+
// Default parameters:
159+
// Default parameters allow you to assign default values to function parameters if no argument is passed.
160+
161+
162+
function multiply(a, b = 1) {
163+
return a * b;
164+
}
165+
166+
console.log(multiply(5)); // prints 5
167+
console.log(multiply(5, 2)); // prints 10

0 commit comments

Comments
 (0)