Skip to content

Commit ff9e7c3

Browse files
committed
task related spred rest destructuring
1 parent d079b15 commit ff9e7c3

3 files changed

Lines changed: 437 additions & 0 deletions

File tree

Programs/task1.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
3+
4+
5+
/*
6+
1. Remove the Middle Elements
7+
Given: const arr = [10, 20, 30, 40, 50, 60];
8+
*/
9+
const arr = [10, 20, 30, 40, 50, 60];
10+
arr.splice(2, 1); // remove 2 elements starting from index 2
11+
console.log(arr); // [10, 20, 50, 60]
12+
13+
14+
15+
/*
16+
2. Split Array into Two Halves
17+
*/
18+
19+
const arr1 = [10, 20, 30, 40, 50, 60];
20+
const mid = (arr1.length / 2);
21+
22+
const firstHalf = arr1.slice(0, mid);
23+
const secondHalf = arr1.slice(mid);
24+
25+
console.log(firstHalf); // [10, 20, 30]
26+
console.log(secondHalf); // [40, 50, 60]
27+
28+
/*
29+
3. Find the First Duplicate
30+
Given: const nums = [1, 3, 4, 2, 3, 5, 6];
31+
*/
32+
33+
34+
35+
const nums = [1, 3, 4, 2, 3, 5, 6];
36+
37+
const dup=nums.find((num,index)=>nums.indexOf(num)!==index);
38+
console.log(dup)
39+
40+
41+
42+
43+
/*
44+
45+
4. const scores = [
46+
  { name: "A", marks: 90 },
47+
  { name: "B", marks: 60 },
48+
  { name: "C", marks: 75 },
49+
  { name: "D", marks: 95 }
50+
];
51+
Task:
52+
Use filter() to get students with marks ≥ 80.
53+
Use map() to return a string like: "A (90 marks)", "D (95 marks)
54+
*/
55+
const scores = [
56+
{ name: "A", marks: 90 },
57+
{ name: "B", marks: 60 },
58+
{ name: "C", marks: 75 },
59+
{ name: "D", marks: 95 }
60+
];
61+
const filtered = scores.filter(scores => scores.marks >= 80);
62+
const mapped = filtered.map(scores=> `${scores.name} (${scores.marks} marks)`);
63+
console.log(filtered);
64+
console.log(mapped);
65+
66+
67+
68+
/*
69+
70+
5. Find Student Who Failed
71+
 
72+
Given:
73+
const results = [
74+
  { name: "Tom", marks: 85 },
75+
  { name: "Jerry", marks: 33 },
76+
  { name: "Spike", marks: 55 }
77+
];
78+
*/
79+
80+
const results = [
81+
{ name: "Tom", marks: 85 },
82+
{ name: "Jerry", marks: 33 },
83+
{ name: "Spike", marks: 55 }
84+
];
85+
const findFailedStudents = results.filter(results=>results.marks<40)
86+
console.log(findFailedStudents)
87+
88+
89+
/*
90+
91+
6. Array Of Object Name Age
92+
>=18 adult
93+
*/
94+
95+
96+
const names=[
97+
98+
{name: "Puja",age:10},
99+
{name :"Shalini",age:20},
100+
{name :"Swagata",age:25}
101+
];
102+
103+
const adult = names.filter(p=>p.age>=18).map(p=>p.name)
104+
console.log(adult);
105+
106+
107+
/*
108+
Diff between Find Filter Map
109+
110+
*/

Programs/task2.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
3+
4+
5+
6+
/**
7+
* const obj1 = {
8+
  user: { name: 'Alice', age: 25 },
9+
  isAdmin: false
10+
};
11+
 
12+
const obj2 = {
13+
  user: { age: 30 },
14+
  isAdmin: true
15+
};
16+
 
17+
// Expected output:
18+
{
19+
  user: { name: 'Alice', age: 30 },
20+
  isAdmin: true
21+
}
22+
*/
23+
/*
24+
Spread operator
25+
*/
26+
27+
const obj1 = {
28+
user: { name: 'Alice', age: 25 },
29+
isAdmin: false
30+
};
31+
32+
const obj2 = {
33+
user: { age: 30 },
34+
isAdmin: true
35+
};
36+
37+
// Merging obj1 and obj2
38+
const merged = {
39+
40+
...obj1,...obj2,
41+
user:{
42+
...obj1.user,
43+
...obj2.user
44+
}
45+
};
46+
47+
console.log(merged)
48+
49+
/*
50+
51+
Destrcturing
52+
53+
54+
//Array destructuring
55+
let arr = [1, 2, 3];
56+
57+
// Destructuring the array into variables
58+
let [a, b, c] = arr;
59+
60+
console.log(a); // Output: 1
61+
console.log(b); // Output: 2
62+
console.log(c); // Output: 3
63+
64+
65+
66+
const user = {
67+
  name: 'Sarah',
68+
  age: 28,
69+
  address: {
70+
    city: 'New York',
71+
    zip: '10001'
72+
  }
73+
};
74+
75+
*/
76+
77+
const user = {
78+
name: 'Sarah',
79+
age: 28,
80+
address: {
81+
city: 'New York',
82+
zip: '10001'
83+
}
84+
};
85+
86+
const{name ,age,address:{city}}=user
87+
88+
console.log(name)
89+
console.log(age)
90+
console.log(city)
91+
92+
93+
/*
94+
95+
const obj1 = {
96+
  user: {
97+
    name: 'Alice',
98+
    details: { age: 25, city: 'Delhi' }
99+
  }
100+
};
101+
 
102+
const obj2 = {
103+
  user: {
104+
    details: { city: 'Mumbai', country: 'India' }
105+
  }
106+
};
107+
 
108+
// Expected Output:
109+
{
110+
  user: {
111+
    name: 'Alice',
112+
    details: {
113+
      age: 25,
114+
      city: 'Mumbai',
115+
      country: 'India'
116+
    }
117+
  }
118+
}
119+
*/
120+
121+
122+
const obj3 = {
123+
userone: {
124+
nameone: 'Alice',
125+
details: { ageone: 25, cityone: 'Delhi' }
126+
}
127+
};
128+
129+
const obj4 = {
130+
userone: {
131+
details: { cityone: 'Mumbai', country: 'India' }
132+
}
133+
};
134+
135+
136+
137+
138+
139+
const mgr={
140+
...obj3.userone,
141+
142+
userone:{
143+
...obj3.userone.details,
144+
...obj4.userone.details
145+
}
146+
};
147+
148+
console.log(mgr)

0 commit comments

Comments
 (0)