Skip to content

Commit b83dbd7

Browse files
committed
feb 12 night
1 parent b12aa8f commit b83dbd7

File tree

1 file changed

+152
-12
lines changed

1 file changed

+152
-12
lines changed

js_notes/basic-q.js

+152-12
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,27 @@ Mixed Topics:
2121
Write a program to find the factorial of a number using a loop.
2222
Create a simple calculator program that performs addition, subtraction, multiplication, and division based on user input
2323
24-
Some extra advanced questions
24+
Some extra advanced questions on functions
2525
1.Implement a JavaScript program to print Fibonacci series using a for loop.
26+
2.Write a JavaScript function to find the largest number among three numbers using nested if-else statements.
27+
3.Write a JavaScript function to calculate the factorial of a number.
28+
4.Implement a JavaScript function to check if a number is prime or not.
29+
5.Write a JavaScript function to reverse a string.
30+
31+
Some extra advanced questions on arrays
32+
1.Write JavaScript code to find the sum of elements in an array.
33+
2.Implement a function to find the maximum and minimum elements in an array.
34+
3.Write JavaScript code to remove duplicate elements from an array.
35+
36+
37+
Questions based on Objects
38+
1.Create a JavaScript object representing a car with properties like make, model, year, and color.
39+
2.Write a method to display all properties of the car object.
40+
3.Implement a function to calculate the total cost of maintenance for the car based on its age and mileage.
2641
*/
2742

43+
44+
2845
//Q1
2946
let num = 10;
3047
let str = "javascript";
@@ -144,15 +161,138 @@ if(inputoperation == "division"){
144161
}
145162

146163
//Q10 - fibonacci series using for loops
147-
//fibonacci series have a series of numbers and each number in the series is equivalent to the sum of the previous 2 numbers
148-
let fibonacciTermsreq = 10;
149-
let t1 = 0;
150-
let t2 = 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+
let terms = 10; //defining the number of terms req
168+
let t1 = 0; //let 1st term is 0
169+
let t2 = 1; //let 2nd term is 1
170+
console.log(t1);//logging t1 and t2
152171
console.log(t2);
153-
for(let i=1;i <= fibonacciTermsreq;++i){
154-
t1 = t2;
155-
t2 = i;
156-
let nextterm = t1 + t2;
157-
console.log(nextterm);
158-
}
172+
for(let i=1;i<=terms;i++){ //loop to iterate and form the fibonacci series
173+
let nextterm = 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+
function largestOf3(num1,num2,num3){
182+
if(num1>num2 && num1>num3){
183+
console.log(`${num1} is greatest`);
184+
}
185+
else if(num2>num1 && num2>num3){
186+
console.log(`${num2} is greatest`);
187+
}
188+
else if(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+
function factorial(num){
199+
let ans =1;
200+
for(let i=1;i<=num;i++){
201+
ans *= i;
202+
}
203+
return ans;
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+
function primechecker(num){
212+
for(let i=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+
function strRev(str){
227+
let arrayOfletters = str.split("");
228+
let revString = "";
229+
for(let i=arrayOfletters.length-1;i>=0;i--){
230+
revString += arrayOfletters[i];
231+
}
232+
return revString;
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+
let sum = 0;
238+
function sumArray(arr){
239+
for(let i=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+
function arrayMaxMin(arr){
249+
let max = arr[0];
250+
for(let i=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+
let min = arr[0];
255+
for(let j=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+
function removeDuplicate(arr){
275+
for(let i=0;i<arr.length;i++){
276+
let letter = arr[i];
277+
for(let j = i + 1; j < arr.length; j++){
278+
if(letter === arr[j]) {
279+
arr.splice(j, 1);
280+
j--;
281+
}
282+
}
283+
}
284+
return arr;
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.
291+
let car = {
292+
made : "BMW",
293+
model : "sedan",
294+
year : 2004,
295+
color : "black",
296+
}
297+
console.log(car);
298+

0 commit comments

Comments
 (0)