-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecmascript6.js
131 lines (96 loc) · 2.58 KB
/
ecmascript6.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
//exercise 1
//Write the destructuring assignment that reads:
//name property into the variable name.
//years property into the variable age.
//isAdmin property into the variable isAdmin (false if absent)
let user = {
name: "John",
years: 30,
};
let name = user.name;
let age = user.years;
let isAdmin = user.idAdmin !== undefined ? user.isAdmin : false;
console.log(isAdmin)
//exercise 2
//Give the right name:
//Create the variable with the name of our planet. How would you name such a variable?
//Create the variable to store the name of the current visitor. How would you name that variable?
let planetEarth;
let currentVisitor;
//exercise 3
//Look at the code. What will be the result of the call at the last line and why?
// let phrase = "Hello"
if (true) {
let user = "John";
function sayHi() {
alert(`${phrase}, ${user}`)
}
}
sayHi()
//output would be error, there cause the function sayHi()is inside th if block
//exercise 4
//Write the code, one line for each action:
//create an empty object user.
//Add the property name with the value John.
//Add the property surname with the value Smith.
//Change the value of the name to Pete.
//Remove the property name from the object.
let user = {
name: "john",
surname: "smith",
};
let str = user.name.replace("john", "pete");
delete user.name;
console.log(user);
//Exercise 5
//Is it possible to change an object declared with const, how do you think and why?
const user = {
name: "John"
}
// does it work?
user.name = "Pete"
// yes this will work, cause you are changing the object inside the variable
//Exercise 6
//We have an object storing the salaries of our team:
//Write a code to sum all salaries and store in the variable sum. If salaries is empty, then the result must be 0.
let salaries = {
Fred: 100,
Ted: 160,
Ghaith: 130,
};
// let sum= salaries.Fred + salaries.Ted + salaries.Ghaith
let sum = 0;
for (let key in salaries) {
sum += salaries[key];
}
console.log(sum);
//exercise 7
//Rewrite this if using the ternary operator '?':
if (a + b < 4) {
result = "Below";
} else {
result = "Over";
}
let result = a + b < 4 ? "below" : "over";
console.log(result);
//exercise 8
//Rewrite if..else using multiple ternary operators '?'.
let message;
if (login == 'Employee') {
message = 'Hello';
} else if (login == 'Director') {
message = 'Greetings';
} else if (login == '') {
message = 'No login';
} else {
message = '';
}
let message =
login == "Employee"
? "Hello"
: login == "Director"
? "Greetings"
: login == ""
? "No login"
: "";
console.log(message);