You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//fibonacci series have a series of numbers and each number in the series is equivalent to the sum of the previous 2 numbers
148
-
letfibonacciTermsreq=10;
149
-
lett1=0;
150
-
lett2=1;
151
-
console.log(t1);
164
+
//fibonacci series have a series of numbers and each number in the series is equivalent to the sum of the previous 2 numbers
165
+
166
+
167
+
letterms=10;//defining the number of terms req
168
+
lett1=0;//let 1st term is 0
169
+
lett2=1;//let 2nd term is 1
170
+
console.log(t1);//logging t1 and t2
152
171
console.log(t2);
153
-
for(leti=1;i<=fibonacciTermsreq;++i){
154
-
t1=t2;
155
-
t2=i;
156
-
letnextterm=t1+t2;
157
-
console.log(nextterm);
158
-
}
172
+
for(leti=1;i<=terms;i++){//loop to iterate and form the fibonacci series
173
+
letnextterm=t1+t2;// fibonacci rule - each term is sum of previous 2 terms t1 and t2
174
+
t1=t2;//now t1 updated to t2
175
+
t2=nextterm;//t2 updated to nextterm value
176
+
console.log(nextterm)//logging the nextterm
177
+
}
178
+
179
+
// Q11- function to find largerst of 3 numbers
180
+
181
+
functionlargestOf3(num1,num2,num3){
182
+
if(num1>num2&&num1>num3){
183
+
console.log(`${num1} is greatest`);
184
+
}
185
+
elseif(num2>num1&&num2>num3){
186
+
console.log(`${num2} is greatest`);
187
+
}
188
+
elseif(num3>num1&&num3>num2){
189
+
console.log(`${num3} is greatest`);
190
+
}
191
+
else{
192
+
console.log("there is not a single greatest number");
193
+
}
194
+
}
195
+
largestOf3(2,200,20);
196
+
197
+
// Q12 - make a fn to calulate factorial of a number
198
+
functionfactorial(num){
199
+
letans=1;
200
+
for(leti=1;i<=num;i++){
201
+
ans*=i;
202
+
}
203
+
returnans;
204
+
}
205
+
console.log(factorial(5));
206
+
207
+
//here we used console.log statement to log the function because the function is returning ans so to log the returned value we are using console.log , if we log the return statement inside the function itself it doesnt rquire logging while calling the function , we can just call the function and pass the arguments as last examples
208
+
209
+
//Q13 - Implement a JavaScript function to check if a number is prime or not.
210
+
211
+
functionprimechecker(num){
212
+
for(leti=2;i<num;i++){
213
+
if(num%i===0){
214
+
return`${num} is not prime`;
215
+
}
216
+
}
217
+
return`${num} is prime`
218
+
}
219
+
console.log(primechecker(25));
220
+
console.log(primechecker(2));
221
+
console.log(primechecker(3));
222
+
console.log(primechecker(234));
223
+
console.log(primechecker(23));
224
+
225
+
// Q-14 Write a JavaScript function to reverse a string.
226
+
functionstrRev(str){
227
+
letarrayOfletters=str.split("");
228
+
letrevString="";
229
+
for(leti=arrayOfletters.length-1;i>=0;i--){
230
+
revString+=arrayOfletters[i];
231
+
}
232
+
returnrevString;
233
+
}
234
+
console.log(strRev("Indresh is a good boy"));
235
+
236
+
//Q15 Write JavaScript code to find the sum of elements in an array.
237
+
letsum=0;
238
+
functionsumArray(arr){
239
+
for(leti=0;i<arr.length;i++){
240
+
sum+=arr[i];
241
+
}
242
+
console.log(sum);
243
+
}
244
+
sumArray([1,2,3,4,5]);
245
+
246
+
// Q16 Implement a function to find the maximum and minimum elements in an array.
247
+
248
+
functionarrayMaxMin(arr){
249
+
letmax=arr[0];
250
+
for(leti=0;i<arr.length;i++){
251
+
arr[i]>max ? max=arr[i]:"";
252
+
}
253
+
console.log(`${max} is the largest element in he array`);
254
+
letmin=arr[0];
255
+
for(letj=0;j<arr.length;j++){
256
+
arr[j]<min ? min=arr[j]:"";
257
+
}
258
+
console.log(`${min} is the smallest element in he array`);
259
+
}
260
+
arrayMaxMin([1,2,3,4,5]);
261
+
262
+
263
+
// Q17 Write JavaScript code to remove duplicate elements from an array.
264
+
/*function removeDuplicate(arr){
265
+
for(let i=0;i<arr.length;i++){
266
+
let letter = arr[i];
267
+
for(let j=0;j<arr.length;j++){
268
+
letter == arr[j] ? arr.splice(j,1) : "";
269
+
}
270
+
}
271
+
}
272
+
removeDuplicate([1,1,2,3,4,4,3]);*/
273
+
274
+
functionremoveDuplicate(arr){
275
+
for(leti=0;i<arr.length;i++){
276
+
letletter=arr[i];
277
+
for(letj=i+1;j<arr.length;j++){
278
+
if(letter===arr[j]){
279
+
arr.splice(j,1);
280
+
j--;
281
+
}
282
+
}
283
+
}
284
+
returnarr;
285
+
}
286
+
287
+
removeDuplicate([1,1,2,3,4,4,3]);
288
+
289
+
290
+
// Q18 Create a JavaScript object representing a car with properties like make, model, year, and color.
0 commit comments