-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathexercise.smart-ease.js
More file actions
148 lines (115 loc) · 3.43 KB
/
exercise.smart-ease.js
File metadata and controls
148 lines (115 loc) · 3.43 KB
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
//The full name exercise.
function getFullName(firstName, lastName,useFormalName ) {
let fullName =` ${firstName} ${lastName}`
return useFormalName ? `Lord ${fullName}` : fullName;
}
const fullName1 = getFullName("John", "Doe");
const fullName2 = getFullName("Jane", "Smith");
console.log(fullName1);
console.log(fullName2);
// Event application
function getEventWeekday(numberOfDaysFromToday) {
const today = new Date();
const targetDate = new Date(today);
targetDate.setDate(today.getDate() + numberOfDaysFromToday);
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const weekday = weekdays[targetDate.getDay()];
return weekday;
}
console.log(getEventWeekday(9));
// Weather wear
function getWearByWeather(temperature) {
if (temperature < 0) {
return "Heavy winter coat, gloves, and a hat";
} else if (temperature >= 0 && temperature < 10) {
return "Winter jacket and warm layers";
} else if (temperature >= 10 && temperature < 18) {
return "Light jacket or sweater";
} else
return "Shorts and a t-shirt";
}
const clothesToWear = getWearByWeather(18);
console.log(clothesToWear);
//Student menager
const class07Students = [];
function addStudentToClass(studentName) {
if (studentName === 'Queen Mary') {
class07Students.push(studentName);
return;
}
if (class07Students.includes(studentName)) {
console.log(`Student ${studentName} is already in the class`);
return;
}
if (studentName=== '') {
console.log('You cannot add an empty name');
return;
}
if(class07Students.length >= 6) {
console.log('Cannot add more students to class 07');
return;
}
class07Students.push(studentName);
}
function getNumberOfStudents() {
return class07Students.length;
}
addStudentToClass('Alice');
addStudentToClass('Bob');
addStudentToClass('Charlie');
addStudentToClass('David');
addStudentToClass('Eve');
addStudentToClass('Frank');
addStudentToClass('Grace');
addStudentToClass('Queen Mary');
addStudentToClass('');
addStudentToClass('Alice');
addStudentToClass('Queen Mary');
console.log(getNumberOfStudents());
//Candy helper
const boughtCandyPrices = [];
function addCandy(candyType, weight){
let price;
if(candyType === "sweet"){
price = 0.5 * weight;
} else if(candyType === "chocolate"){
price = 0.7 * weight;
} else if(candyType === "toffee"){
price = 1.1 * weight;
} else if(candyType === "chewing-gum"){
price = 0.03 * weight;
}
boughtCandyPrices.push(price);
}
let amountToSpend = Math.random() * 100;
function canBuyMoreCandy(){
let totalSpent = 0;
for(let i = 0; i < boughtCandyPrices.length; i++){
totalSpent += boughtCandyPrices[i];
}
return totalSpent < amountToSpend;
}
addCandy("sweet", 10);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}
addCandy("chocolate", 5);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}
addCandy("toffee", 2);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}
addCandy("chewing-gum", 50);
if(canBuyMoreCandy()){
console.log("You can buy more, so please do!");
}else{
console.log("Enough candy for you!");
}