-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasic.js
More file actions
332 lines (251 loc) · 10 KB
/
Basic.js
File metadata and controls
332 lines (251 loc) · 10 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
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
325
326
327
328
329
330
331
332
// Swap the values of two variables without using a third variable.
// Solved using Functions
{
let var1 = 12;
let var2 = 13;
function swap(a,b){
var1 = b;
var2 = a;
console.log(`Value of Var1 : ${var1} and value of Var2 : ${var2}`);
}
swap(var1 , var2)
}
// CHAT GPT METHOD----destructuring assignment
{
let a = 10;
let b = 111;
[a,b]=[b,a]
// Agge vle me value assign horhi hai piche vle k respect me
// jisse pata chalta hai ki a me b ki value aur b me a ki value gai
console.log(a,b);
}
// Write a function that takes an integer as an argument and returns "Even" for even numbers and "Odd" for odd numbers.
{
function everOrOdd(int){
if(int%2==0){
return 'The given argument is Even'; // If remainder is 0, it's an even number
}else{
return 'The given argument is Odd'; // If remainder is not 0, it's an odd number
}
}
let ans = everOrOdd(31);
console.log(ans);
}
// Write a function to calculate the factorial of a given number.
{
let number = 4;
let factorial = (val)=>{
let factorials = 1;
for(let i = 1; i<=val; i++){
factorials *=i;
}
console.log('Factorial of ' ,val,' is ',factorials);
}
factorial(number)
}
// CHAT-GPT ---- Recurssion method with in depth explanation of it--
{
// Function to calculate the factorial of a given number
function calculateFactorial(number) {
// Base case: factorial of 0 is 1
if (number === 0 || number === 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return number * calculateFactorial(number - 1);
}
}
// Example usage:
let result1 = calculateFactorial(5);
console.log("Factorial of 5:", result1); // Output: 120
let result2 = calculateFactorial(3);
console.log("Factorial of 3:", result2); // Output: 6
}
//Explanation--------
// {
// Bilkul, recursion ka concept samajhne ke liye, humein yeh samajhna zaroori hai ki recursion mein function apne aap ko call karta hai, lekin har bar ek chhota input lekar. Yahan, hum dekhte hain kaise `calculateFactorial` function apne aap ko call karta hai:
// ```javascript
// // Recursive case: n! = n * (n-1)!
// return number * calculateFactorial(number - 1);
// ```
// Yeh line batati hai ki agar hume \(n\) ka factorial calculate karna hai, toh hum \(n\) ko uske ek chhote number (\(n-1\)) ke factorial se multiply karenge.
// Let's break it down with an example, maan lijiye \(n = 5\):
// 1. **Step 1:** Hum `calculateFactorial(5)` ko call karte hain.
// 2. **Step 2:** Function check karta hai ki \(n\) agar 0 ya 1 hai toh direct 1 return karta hai. Yahan \(n\) 5 hai, toh base case nahi apply hota.
// 3. **Step 3:** Ab yeh line execute hoti hai: `return 5 * calculateFactorial(4);`
// 4. **Step 4:** Ab, `calculateFactorial(4)` ko call karte hain. Yahan \(n\) 4 hai.
// 5. **Step 5:** Yeh line execute hoti hai: `return 4 * calculateFactorial(3);`
// 6. **Step 6:** Fir, `calculateFactorial(3)` ko call karte hain. Yahan \(n\) 3 hai.
// 7. **Step 7:** Yeh line execute hoti hai: `return 3 * calculateFactorial(2);`
// 8. **Step 8:** Fir, `calculateFactorial(2)` ko call karte hain. Yahan \(n\) 2 hai.
// 9. **Step 9:** Yeh line execute hoti hai: `return 2 * calculateFactorial(1);`
// 10. **Step 10:** Ab, `calculateFactorial(1)` ko call karte hain. Yahan \(n\) 1 hai.
// 11. **Step 11:** Yeh line execute hoti hai: `return 1;` (kya aapne notice kiya, yahan base case lag gaya hai, kyun ki \(n\) 1 hai).
// 12. **Step 12:** Ab, sab ke sab return values ko multiply karte hain: \(5 \times 4 \times 3 \times 2 \times 1 = 120\).
// Is tarah se, recursion ke through hum 5 ka factorial calculate karte hain, jisme har level pe function apne aap ko chhota input dekar call karta hai, jab tak base case tak pahunch jata hai.
// }
// Write a function that takes an array of numbers and returns the sum of all the elements.
{
let arr = [1,2,3,4,5,6,7,8,9];
let sumOfNum = function(val){
let sum=0;
val.forEach(function(elem){
sum+=elem;
});
return sum;
}
console.log(sumOfNum(arr))
}
// Write a function to check if a given word is a palindrome.
{
let num = 121;
function checkPalindrome(val){
let stringify = String(val);
let rev = stringify.split('').reverse().join('');
if(stringify == rev){
return 'The Number is a Palindrome Number';
}else{
return 'The Number is not a Palindrome Number'
}
}
console.log(checkPalindrome(num));
}
// Explanation of Join, Reverse and split Method
{
//REverse Method----------------
// Reverse Method kisi bhi array ko reverse krne k liye use hota hai.
// So, to clarify, in JavaScript, reverse() is a method that operates on arrays, and it is used to reverse the order of elements within the array.
let n = [1,2,3,4];
console.log(n.reverse());
// Split Method -----------------
// split() ek JavaScript method hai jo ek string ko tukdon mein batakar unhe ek array mein convert karta hai. Iska use karke hum kisi bhi string ko specific character ya substring ke basis par alag-alag pieces mein tod sakte hain.
let str = 'abhishek';
console.log(str.split(''))// agr space nahi dnge to har letter ko alg alg kr dega
// Join Method -------------
// join() method JavaScript mein istemal hota hai taki hum array ke elements ko ek string mein jod sakein. Yeh ek optional parameter bhi leta hai, jo separator hota hai jise hum elements ke beech mein rakhna chahte hain. Yeh method original array ko modify nahi karta; balki, ek naya string return karta hai jo array ke elements ko ek saath represent karta ha
console.log(n.join(''));
}
// Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers that are multiples of both three and five, print "FizzBuzz".
{
for(let i = 1; i<=100; i++){
if(i%3==0 && i%5==0){
console.log('FizzBuzz');
}else if(i%5==0){
console.log('Buzz');
}else if(i%3==0){
console.log('Fizz');
} else{
console.log(i);
}
}
}
// Write a function that takes an array of numbers and returns the largest number.
{
let arr = [12,45,101,35,10,78,45,94];
let largestNum = (arrays)=>{
let largest = arrays[0];
for(let i =1; i<arrays.length; i++){
if(arrays[i]>largest){
largest = arrays[i]
}
}
return largest;
}
let ans = largestNum(arr)
console.log(ans);
}
//Write a function to reverse a given string.
{
let str = 'Hello World!!';
let reverseStr = function(strings){
let rev = strings.split('').reverse().join('');
return rev;
}
let reversed = reverseStr(str)
console.log(reversed);
}
// Write a function that takes a string and returns the count of vowels in it.
{
let str = "aeiou";
(function(strs){
let vowels = ['a','e','i','o','u','A','E','I','O','U'];
let vowelCount = 0;
for(let char of strs){
if(vowels.includes(char)){//isme check kiya ki kya strings ke character array me aarhe hai ki nahi
vowelCount++;//true hogi condition to increment operator chal jaega
}
}
console.log(vowelCount);
})(str)
}
// Write a function that determines whether a given number is a prime number.
{
let num = 10;
function checkPrime(a){
for(let i = 2; i<a;i++){
if(a==1){
return "This is a Prime Number"
}else if(a%i==0){
return 'This is Not a Prime Number'
}else{
return 'this is a Prime Number'
}
}
}
let prime = checkPrime(num)
console.log(prime);
}
// Write a function that checks if a given year is a leap year.
{
let year = 2025;
function checkLeapYear(yrs){
if(yrs % 4== 0){
console.log(`${yrs} is a Leap Year`);
}else{
console.log(`${yrs} is not a Leap Year`);
}
}
checkLeapYear(year)
}
// Write a function that takes an array and removes duplicate elements.
{
let arr = [1,2,13,3,4,1,2];
function removeDupli(arry){
let sets = new Set(arr); // St
const uniqueArray = Array.from(sets)
console.log(uniqueArray);
}
removeDupli(arr)
}
// Write a function that takes a sentence and capitalizes the first letter of each word.
{
function capitalizeFirstLetter(sentence) {
// Split the sentence into an array of words
const words = sentence.split(' ');
// Iterate through each word in the array
for (let i = 0; i < words.length; i++) {
// Capitalize the first letter of each word
words[i] = words[i][0].toUpperCase() + words[i].slice(1);
}
// Join the capitalized words back into a sentence
const capitalizedSentence = words.join(' ');
return capitalizedSentence;
}
const sentence = "this is a sample sentence";
const result = capitalizeFirstLetter(sentence);
console.log(result); // Output: "This Is A Sample Sentence"
}
// Write a function that calculates the sum of squares of numbers from 1 to a given number.
{
let num =25;
function sumOfSquare(int){
let sum = 0;
for(let i=1;i<=int;i++){
let square = i**2;
// console.log(square);
sum += square;
}
console.log(sum);
}
sumOfSquare(num);
}