-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 8 ES6+.js
138 lines (106 loc) · 2.88 KB
/
Day 8 ES6+.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Activity 1: Template Literals
//Task 1:
// const name = "John";
// const age = 25;
// console.log(`Name: ${name}, Age: ${age}`);
//Task 2
const multiLineString = `This is a string
that spans multiple
lines using template literals.`;
console.log(multiLineString);
//Activity 2: Destructuring
//Task 3:
const numbers = [1, 2, 3, 4, 5];
const [first, second] = numbers;
console.log(`First: ${first}, Second: ${second}`);
//Task 4:
const book = {
title: "JavaScript: The Good Parts",
author: "Douglas Crockford"
};
const { title, author } = book;
console.log(`Title: ${title}, Author: ${author}`);
//Activity 3: Spread and Rest Operators
//Task 5
const oldArray = [1, 2, 3];
const newArray = [...oldArray, 4, 5, 6];
console.log(newArray);
//Task 6
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3, 4));
// Activity 4: Default Parameters
//Task 7:
function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5)); // Outputs: 5
console.log(multiply(5, 2)); // Outputs: 10
//Activity 5: Enhanced Object Literals
//Task 8:
const name = "sourabh";
const age = 21;
const person = {
name,
age,
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
};
console.log(person);
person.greet();
//Task 9:
const propName = "dynamicProperty";
const obj = {
[propName]: "This is a dynamic property"
};
console.log(obj);
// Feature Request Scripts
//1. Template Literals Script:
const personName = "Alice";
const personAge = 30;
const greeting = `Hello, my name is ${personName} and I am ${personAge} years old.`;
console.log(greeting);
const multiLineGreeting = `This is the first line.
This is the second line.
This is the third line.`;
console.log(multiLineGreeting);
//2. Destructuring Script:
const array = [10, 20, 30, 40, 50];
const [firstValue, secondValue] = array;
console.log(`First value: ${firstValue}, Second value: ${secondValue}`);
const bookObject = {
title: "Eloquent JavaScript",
author: "Marijn Haverbeke"
};
const { title: bookTitle, author: bookAuthor } = bookObject;
console.log(`Title: ${bookTitle}, Author: ${bookAuthor}`);
//3. Spread and Rest Operators Script:
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combinedArray = [...arr1, ...arr2];
console.log(combinedArray);
function calculateSum(...args) {
return args.reduce((sum, current) => sum + current, 0);
}
console.log(calculateSum(1, 2, 3, 4)); // Outputs: 10
//4. Default Parameters Script:
function add(a, b = 5) {
return a + b;
}
console.log(add(10)); // Outputs: 15
console.log(add(10, 20)); // Outputs: 30
//5. Enhanced Object Literals Script:
const prop = "status";
const value = "active";
const user = {
id: 1,
name: "Bob",
[prop]: value,
display() {
console.log(`User: ${this.name}, Status: ${this[prop]}`);
}
};
console.log(user);
user.display();