Skip to content

Commit 937841c

Browse files
committed
feb 11
1 parent 7ac8481 commit 937841c

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

js_notes/basic-q.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*Variables and Data Types:
2+
Declare variables of different data types (string, number, boolean, array, object) and perform operations on them.
3+
4+
5+
Conditional Statements:
6+
Write a program to check if a number is even or odd.
7+
Create a grading system where you input a score and output the corresponding grade (A, B, C, D, or F).
8+
9+
10+
Loops:
11+
Write a program to print the first 10 natural numbers using a loop.
12+
Create a multiplication table for a given number (e.g., 5) using a loop.
13+
14+
15+
Operators:
16+
Write a program to calculate the area of a rectangle given its length and width.
17+
Convert temperature from Celsius to Fahrenheit and vice versa using appropriate operators.
18+
19+
20+
Mixed Topics:
21+
Write a program to find the factorial of a number using a loop.
22+
Create a simple calculator program that performs addition, subtraction, multiplication, and division based on user input*/
23+
24+
//Q1

js_notes/cohert1-v1.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ API - native vs web
2323

2424
//Question1
2525
// sum from 1 to 100
26-
let sum = 0;
26+
let summ = 0;
2727
for(let i=0;i<=100;i++){
28-
sum = sum + i;
28+
summ = summ + i;
2929
}
30-
console.log(sum);
30+
console.log(summ);
3131

3232
//Question2
3333
//Print fibonacci series - it is a series of number where every number is the sum of the preceeding 2 numbers
@@ -146,15 +146,17 @@ console.log(arithmatic(12,23,'+'));
146146

147147

148148
//In the below codebase we are trying to use 2 differnce seperate functions to calculate sum and difference but we use condotional statement just to call those 2 function based on the type of operation user needs to perform like sum or difference
149+
150+
// /Note that in the below codebase we are calling a function inside another function. For eg - function calc is calling 2 functions sum and difference.
149151
function calc(a,b,type){
150152
if(type == "+"){
151-
return sum(a,b);
153+
return plus(a,b);
152154
}
153155
else if(type == "-"){
154156
return difference(a,b);
155157
}
156158
}
157-
function sum(a,b){
159+
function plus(a,b){
158160
return a+b;
159161
}
160162
function difference(a,b){
@@ -199,17 +201,61 @@ console.log(greet); //calling greet function
199201
//setTImeot is a fuction that takes 2 arguments where one of the argument is a function. This is called callbacks
200202

201203

204+
//Are we allowed to call a function inside another fuction - yes
205+
206+
function square(n){
207+
return n*n;
208+
}
209+
function squaresum(a,b){
210+
let val1 = square(a);
211+
let val2 = square(b);
212+
return val1 + val2;
213+
}
214+
console.log(squaresum(4,3));
215+
216+
217+
218+
//Are we allowed to use a function as a parameter for another function - yes and its called callback function
219+
function sq(a){
220+
return a*a;
221+
}
222+
function cb(a){
223+
return a*a*a;
224+
}
225+
function calcsum(num1,num2,callback){
226+
const val1 = callback(num1);
227+
const val2 = callback(num2);
228+
return val1+val2;
229+
}
230+
console.log(calcsum(10,10,sq));
231+
console.log(calcsum(10,10,cb));
232+
233+
//Are we allowed to use a function as a parameter ? - yes and its called anonymous function
234+
235+
//these functoions dont require any function name because they cant be called anywhere lese if they are passes as a parameter , so these nameless functions are called anonymous function
236+
237+
238+
function calcmodulo(a,b,fn){ //3 parameters, 2 numbers and a function
239+
console.log(fn) //we are logging the function to see its type.on line 243 we see the argument directly has the fucnction body . This is called anonymous function
240+
const mod = fn(a,b); //now we pass the fn and save its result in mod variable
241+
return a+b+mod; //return statement
242+
}
243+
console.log(calcmodulo(13,7,function(a,b){ //function as argument has to do the a%b
244+
return a%b;
245+
}))
246+
247+
248+
249+
250+
202251

203252
//Assignment 1
204253
// Question1 - create a counter in js that counts from 30 to 0
205254
// Question2 - calculate the time it takes between the setTimeout call and the inner fuction actually running
206255
// Question3 - create a terminal clock (HH:MM:SS)
207-
function counter(){
208-
for(let i=30;i>0;i--){
209-
console.log(i);
210-
setTimeout(counter,1000);
211-
}
212-
}
213256

214-
console.log(counter);
257+
258+
259+
260+
215261

js_notes/commonfn.js

Whitespace-only changes.

0 commit comments

Comments
 (0)