-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (70 loc) · 3.01 KB
/
index.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
console.log("-------------------------------------------------");
console.log("Activity 1: ");
// Task 1: Use template literals to create a string that includes variables for a person's name and age, and log the string to the console.
let name = "John";
let age = 30;
console.log(`My name is ${name} and I am ${age} years old.`);
// Task 2: Create a multi-line string using template literals and log it to the console.
let multiLineString = `This is a multi-line string.
It is created using template literals.
It is very convenient to use.`;
console.log(`This is multiLineString:- ${multiLineString}`);
console.log("-------------------------------------------------");
console.log("Activity 2: ");
// Task 3: Use array destructuring to extract the first and second elements from an array of numbers and log them to the console.
arr = [1, 2, 3, 4, 5];
let [first, second] = arr;
console.log(`First element: ${first}, Second element: ${second}`);
// Task 4: Use object destructuring to extract the title and author from a book object and log them to the console.
let book = {
title: "The Alchemist",
author: "Paulo Coelho"
};
let {title, author} = book;
console.log(`Title: ${title}, Author: ${author}`);
console.log("-------------------------------------------------");
console.log("Activity 3: ");
// Task 5: Use the spread operator to create a new array that includes all elements of an existing array plus additional elements, and log the new array to the console.
arr1 = [1, 2, 3];
arr2 = [4, 5, 6];
newarr = [...arr1, ...arr2];
console.log(`New Array: ${newarr}`);
// Task 6: Use the rest operator in a function to accept an arbitrary number of arguments, sum them, and return the result.
function sum(...theArgs) {
let total = 0;
for (const arg of theArgs) {
total += arg;
}
return total;
}
console.log(`Sum of 1, 2, 3, 4, 5: ${sum(1, 2, 3, 4, 5)}`);
console.log("-------------------------------------------------");
console.log("Activity 4: ");
// Task 7: Write a function that takes two parameters and returns their product, with the second parameter having a default value of 1. Log the result of calling this function with and without the second parameter.
function product(a, b = 1) {
return a * b;
}
console.log(`Product of 5 and 2: ${product(5, 2)}`);
console.log("-------------------------------------------------");
console.log("Activity 5: ");
// Task 8: Use enhanced object literals to create an object with methods and properties, and log the object to the console.
let person = {
name: "Linus",
age: 30,
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
};
person.greet();
// Task 9: Create an object with computed property names based on variables and log the object to the console.
const prop1 = "firstName";
const prop2 = "lastName";
const prop3 = "age";
// Create an object with computed property names
const details = {
[prop1]: "John",
[prop2]: "Doe",
[prop3]: 30
};
// Log the object to the console
console.log(details);