Skip to content

Commit 7ac8481

Browse files
committed
10feb
1 parent a56f742 commit 7ac8481

File tree

4 files changed

+186
-4
lines changed

4 files changed

+186
-4
lines changed

.DS_Store

6 KB
Binary file not shown.

js_notes/basic-q.js

Whitespace-only changes.

js_notes/cohert1-v1.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,172 @@ for(let i=3;i<=terms;++i){ //iterating form term 3
4444
nextterm = t1+t2; //now add the new 2 preceding terms
4545
}
4646

47+
48+
//ARRAYS
49+
50+
//arrays - They are a datastructure which grps similar kinds if data
51+
// declaring a array in js
52+
let ages = [12,13,14,56,34,67];
53+
//printing only even ages from the array
54+
for(let i=0;i<=ages.length;i++){
55+
ages[i]%2==0 ? console.log(ages[i]) : "";
56+
}
57+
58+
//write a code to pint the biggest number in an array
59+
let max = 0;
60+
for(let i=0;i<ages.length;i++){
61+
ages[i]>max ? max = ages[i] : "";
62+
}
63+
console.log(`The maximum number is ${max}`);
64+
65+
//reverse an array
66+
let num=[1,2,3,4,5];
67+
let revnum=[];
68+
for(let i=0;i<num.length;i++){
69+
revnum[(num.length-1)-i]=num[i];
70+
}
71+
console.log(`the reversed array is ${revnum}`);
72+
73+
74+
//OBJECTS
75+
76+
//print only the male candidates from a complex objects
77+
// here we are using object of arrays and to acces those array elements in the object we use the .(dot) operator
78+
let users={
79+
firstname:["indresh","rohan","radha","rama","shiva"],
80+
gender:["male","male","female","female","male"]
81+
}
82+
for(let i=0;i<users.firstname.length;i++){
83+
users.gender[i]=="male"?console.log(users.firstname[i]):"";
84+
}
85+
86+
//using array of objects print only the male candidates above age 18 from a complex objects
87+
let userinfo = [
88+
{
89+
username:"indresh",
90+
gender:"male",
91+
metadata:{
92+
phone:123456789,
93+
address:"asdfgg",
94+
age:20,
95+
}
96+
},
97+
{
98+
username:"shakthi",
99+
gender:"female",
100+
metadata:{
101+
phone:12356789,
102+
address:"asdg",
103+
age:22,
104+
}
105+
},
106+
{
107+
username:"rohan",
108+
gender:"male",
109+
metadata:{
110+
phone:2356789,
111+
address:"dfgg",
112+
age:10,
113+
}
114+
}
115+
]
116+
for(let i=0;i<userinfo.length;i++){
117+
userinfo[i]["gender"]=="male" && userinfo[i]["metadata"]["age"]>18 ? console.log(`male user above 18 is ${userinfo[i]["username"]}`):"";
118+
}
119+
120+
//FUNCTIONS
121+
// functions let you abstract out logic out of your program
122+
// They take argument as input and return value as an output
123+
//we can think of them as independent program that is supposed to do something when given an input
124+
//functions can take other functions as input this is called callbacks
125+
126+
127+
//write a function to calculate sum of 2 numbers , let a and b be the parameters and call the funvction with the arguments 12 and 34
128+
function sum(a,b){
129+
let ans = a+b;
130+
return ans;
131+
}
132+
let sum1 = sum(12,34);
133+
console.log(sum1);
134+
135+
//creating a function that takes input of 2 numbers and the oprator to provide the solution for the arithematic expression
136+
function arithmatic(a,b,type){
137+
if(type == "+"){
138+
return a+b;
139+
}
140+
else if(type == "-"){
141+
return a-b;
142+
}
143+
}
144+
console.log(arithmatic(12,23,'+'));
145+
//In the above codebase we directly used the arithematic operations in the conditional stetements itelf.
146+
147+
148+
//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+
function calc(a,b,type){
150+
if(type == "+"){
151+
return sum(a,b);
152+
}
153+
else if(type == "-"){
154+
return difference(a,b);
155+
}
156+
}
157+
function sum(a,b){
158+
return a+b;
159+
}
160+
function difference(a,b){
161+
return a-b;
162+
}
163+
console.log(calc(10,10,"-"));
164+
165+
166+
//now instead of using all these conditional statements we are going to use these functins as an argument and this is called callbacks
167+
168+
//here we are giving functions add and sub as a parameter and based on the input the fn is called
169+
170+
//Callbacks functions can be useful only if the 2 functions viz the function viz calling and the function viz called both have same parameters. Callbacks are not used at places where functions have different parameters
171+
function calculator(num1,num2,operationfn){
172+
let ans = operationfn(num1,num2);
173+
return ans;
174+
}
175+
function add(num1,num2){
176+
return num1+num2;
177+
}
178+
function sub(num1,num2){
179+
return num1-num2;
180+
}
181+
console.log(calculator(10,20,sub));
182+
183+
184+
//another example of callback function can be understood using setTimeout function
185+
186+
//function to greet someone
187+
function greet(){
188+
console.log("hello world");
189+
}
190+
setTimeout(greet,3*1000);
191+
console.log(greet); //calling greet function
192+
193+
194+
//This setTimeout is a function that takes 2 arguments , one is the function it needs to execute and teh other is the time it shd wait ir freez before executing that function. Here 3*1000 - is 3 sec so it means wait for 3 sec before executing the greet function
195+
196+
197+
//basically if u run this program we can see that greet function is executed only after 3 sec of running the program. so here
198+
199+
//setTImeot is a fuction that takes 2 arguments where one of the argument is a function. This is called callbacks
200+
201+
202+
203+
//Assignment 1
204+
// Question1 - create a counter in js that counts from 30 to 0
205+
// Question2 - calculate the time it takes between the setTimeout call and the inner fuction actually running
206+
// 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+
}
213+
214+
console.log(counter);
215+

js_notes/patternQ.js renamed to js_notes/loops&patternQ.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for(let j=0;j<=10;j++){
1515
//if you dont have any else block you still have to complete the syntax using "" as above
1616
}
1717
//using while loop
18-
let num = 0;
18+
let num = 0;
1919
while(num<=10){
2020
num%2==0 ? console.log(num) : ""
2121
num++;
@@ -50,6 +50,17 @@ for(let i=1;i<=2;i++){
5050
}
5151
}
5252

53+
/* why are pattern important
54+
1. enhances thinking and problem solving skills
55+
56+
How to approach solving these pattern questions
57+
1. Run the outer for loop the no.of times of rows / no.of lines
58+
no.of lines = no.of rows = no.of times the outer loop will run
59+
2. Identify for every row number how many columns are there.
60+
3. Undertand the type of elements in the column like * or numbers etc..
61+
62+
*/
63+
5364

5465

5566
//Print a pattern if starts using nested loops
@@ -222,14 +233,16 @@ for(let i=1;i<=rows;i++){ //outerloop for no.of rows
222233
console.log(pattern);
223234
}
224235

236+
//HOW TO GET USER INPUT FROM TERMINAL IN JS :
237+
//First, you need to import the readline module:
225238
const readline = require('readline');
226-
239+
//Then, create an interface to read input from the user:
227240
const rl = readline.createInterface({
228241
input: process.stdin,
229242
output: process.stdout
230243
});
231-
244+
//Now, you can use the question() method to prompt the user for input:
232245
rl.question("Enter something: ", function(userInput) {
233246
console.log("You entered:", userInput);
234-
rl.close();
247+
rl.close(); //Finally, don't forget to close the interface after reading the input to avoid memory leaks:
235248
});

0 commit comments

Comments
 (0)