-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (184 loc) · 7.4 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//ES Exercises ------------------------------>>>
// 1- Use the let keyword in an example function
// let num1=10; //global variable for sum function
// let num2=20;
// function sum(){
// let num1=30; //local varibale
// return num1+num2;
// }
// console.log('Sum: ',sum());
// ----------------------------------------------------------
// 2- Use the const keyword in an example function
// const PI=3.14;
// PI=45.9;
// console.log('Value of PI: ', PI);
// ----------------------------------------------------------
// 3- Create an arrow function that finds the square of a number
// const squareOfNum=(num)=>{
// return num*num;
// }
// console.log('Square of 4: ',squareOfNum(4));
// --------------------------------------------------------------------------
// 4- Create an arrow function that adds two numbers
// let num1=3,num2=4;
// const sumOfTwoNum=(num1,num2)=>{
// return num1+num2;
// }
// const result=sumOfTwoNum(num1,num2);
// console.log(`Sum of ${num1} & ${num2} = ${result}`);
// --------------------------------------------------------------------------
// 5- Create a multi-line string and then split the string into the corresponding lines and print the lines
// let str=`
// Hello ,World !
// Javascript is good Programming Lanugage,
// It is very popular for Web development.
// `
// let sentences=str.split('\n');
// console.log(sentences);
// sentences.forEach((sentence)=>{
// if(sentence){
// console.log(sentence);
// }
// })
// --------------------------------------------------------------------------
/* 6- Create a function that calculates the area of a circle.
If the radius of the circle is not provided assume that the default radius is 5.
Use the JavaScript default parameter feature to implement the function
*/
// const areaOfCircle=(radius=2)=>{
// return (Math.PI*radius*radius).toFixed(2);
// }
// console.log('Area of Circle for 4 : ',areaOfCircle(4));
// console.log('Area of Circle : ',areaOfCircle());
// --------------------------------------------------------------------------
/*7- Create a string that prints the name and location of the person in below format:
"Harry Potter is located in London."
using template literals
*/
// let person = {
// name: 'Harry Potter',
// location: 'London'
// }
// const str=`${person.name} is located in ${person.location}.`
// console.log(str);
// ----------------------------------------------------------------------------------------
// 8- Show an example where an array is destructured using destructuring assignment
// let ages=[12,24,10,40];
// let [rahulAge,shyamAge,akashAge,ramAge]=ages; //destructuring ages array
// console.log(`Rahul's age is ${rahulAge} `);
// console.log(`Shyam's age is ${shyamAge} `);
// console.log(`Akash's age is ${akashAge} `);
// console.log(`Ram's age is ${ramAge} `);
// ----------------------------------------------------------------------------------------
//9 - Show an example where an object is destructured using destructuring assignment in the function body
// let person={
// name:'Akash',
// age:23,
// role:'Software Developer'
// }
// function showInfo(person){
// let {name,age,role}=person; //destructuring the person object
// console.log(`${name} is ${age} yr old and He works as ${role}.`);
// }
// showInfo(person);
// ----------------------------------------------------------------------------------------
//10 - Show an example where a function argument which is an object is destructured inside the parantheses of the function
// let keyboard ={
// brandName:'Logitech G915 TKL',
// isWireless:true,
// isMechanical:true,
// isBacklit:true,
// color:'black'
// }
// function getSpecification({brandName,isWireless,isMechanical,isBacklit,color}){
// const backlit=isBacklit?'backlit':'no backlit';
// const mechanical=isMechanical?'mechanical':'no mechanical';
// const wireless=isWireless?'wireless':'no wireless';
// const info=`${brandName} is ${color} keyboard that comes with ${backlit} , ${mechanical} ,${wireless}.`;
// return info;
// }
// console.log(getSpecification(keyboard));
// ----------------------------------------------------------------------------------------
//11- Show an example where enhanced object literals is used.
// let name='Ram';
// let experience=5;
// let role='Software Developer';
// let showInfo=function (){
// console.log(`${this.name} has ${this.role} experiece of ${this.experience} yrs.`);
// }
// let person1={name,experience,role,showInfo}; //using enhanced object literals to combine all varibales into person1 object
// person1.showInfo();
// ----------------------------------------------------------------------------------------
/* 12 - Create a function sum that takes any number of numbers as arguments and
calculates the sum of the input numbers using the rest parameter syntax
*/
// function sum(...args) {
// console.log('Args :' ,args);
// const result = args.reduce((prevSum, curValue) => {
// return (prevSum = prevSum + curValue);
// });
// return result;
// }
// console.log('Sum of 2 & 3 : ', sum(2, 3));
// console.log('Sum of 2, 3 & 4 : ', sum(2, 3, 4));
// console.log('Sum of 2,3,4 & 5 : ', sum(2, 3, 4, 5));
// ----------------------------------------------------------------------------------------
/* 13- Use the spread syntax to expand an array of numbers and
pass the elements of the array as arguments to the sum function created in the previous example
*/
// function sum(num1,num2){
// return num1+num2;
// }
// function sum(num1,num2,num3=0){
// return num1+num2+num3;
// }
// let nums1=[1,2];
// let nums2=[1,2,3];
// console.log('Sum of 1 & 2 : ',sum(...nums1)); //spreading array into individuals num
// console.log('Sum of 1,2 & 3 : ',sum(...nums2));
// ----------------------------------------------------------------------------------------
// 14- Use the for..of loop to iterate through all values in an array
// let names=['ram','shyam','sohan','manish'];
// for(let name of names){
// console.log(name);
// }
// ----------------------------------------------------------------------------------------
// 15- Iterate through all keys of an object using Object.keys
// let person={
// name:'Akash',
// age:23,
// role:'Software Developer',
// address:'ShuklaGanj Kanpur'
// }
// const personKeys=Object.keys(person);
// console.log('Keys of Person object :');
// personKeys.forEach((key)=>{
// console.log(key);
// })
// ----------------------------------------------------------------------------------------
//16- Iterate through all values of an object using Object.values
// let person={
// name:'Akash',
// age:23,
// role:'Software Developer',
// address:'ShuklaGanj Kanpur'
// }
// const personValues=Object.values(person);
// console.log('Values of Person object :');
// personValues.forEach((value)=>{
// console.log(value);
// })
// ----------------------------------------------------------------------------------------
//17- Iterate through all the key / value pairs of an object using Object.entries
let person = {
name: 'Akash',
age: 23,
role: 'Software Developer',
address: 'ShuklaGanj Kanpur',
};
const personKeyValuesPair = Object.entries(person);
console.log('All the keys / values of Person object : ');
personKeyValuesPair.forEach((property) => {
const [key, value] = property; //destructuring into key and value
console.log(`${key} : ${value}`);
});