forked from mouredev/hello-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09-conditionals-exercises.js
More file actions
227 lines (217 loc) · 7.06 KB
/
09-conditionals-exercises.js
File metadata and controls
227 lines (217 loc) · 7.06 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
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
/*
Clase 24 - Ejercicios: Condicionales
Vídeo: https://youtu.be/1glVfFxj8a4?t=8652
*/
// if/else/else if/ternaria
// 1. Imprime por consola tu nombre si una variable toma su valor
console.log('Exercise 1')
let myName = 'Daniel'
let myLastName = 'Builes'
let youNameIs = `Hi ${myName} how you're doing?`
if(myName == 'Daniel'){
console.log(youNameIs)
}
if (myLastName == 'Builes'){
console.log(`Your last name is ${myLastName}`)
}
console.log('---------------------------------------------------------')
// 2. Imprime por consola un mensaje si el usuario y contraseña concide con unos establecidos
console.log('Exercise 2')
let user = 'dani'
let password = '123'
if(user == 'dani' && password == '123'){
console.log('welcome mr')
} else{
console.log('You user or password are incorrect')
}
console.log('---------------------------------------------------------')
// 3. Verifica si un número es positivo, negativo o cero e imprime un mensaje
console.log('Exercise 3')
let number = -7
if(number == 0){
console.log('your number is 0')
} else if(number < 0){
console.log('You put a negative number')
} else{
console.log('your put a positive number')
}
console.log('--------------------------------------------------------------------------------------')
// 4. Verifica si una persona puede votar o no (mayor o igual a 18) e indica cuántos años le faltan
console.log('Exercise 4')
let age = 15
let legalAge = 18
let ageNecesary = legalAge - age
let message = `You will be able to vote in ${ageNecesary} years more`
if (age >= legalAge){
console.log('You are able to vote')
} else if (age < 0){
console.log('Please enter a correct value')
} else{
console.log(message)
}
console.log('------------------------------------------------------------------------------------------------')
// 5. Usa el operador ternario para asignar el valor "adulto" o "menor" a una variable
// dependiendo de la edad
console.log('Exercise 5')
let yourAge = 16;
// usaré la varialble legalAge del ejercicio anterior
let youAre = yourAge >= legalAge ? 'You are over age' : 'You are under age'
console.log(youAre)
console.log('----------------------------------------------------------------------------------------------------')
// 6. Muestra en que estación del año nos encontramos dependiendo del valor de una variable "mes"
console.log('Exercise 6')
let mounth = 2
if (mounth >= 1 && mounth <= 3){
console.log('You chose Winter season')
} else if (mounth > 3 && mounth <= 6){
console.log('You chose Spring season')
} else if (mounth > 6 && mounth <= 9){
console.log('You chose Summer season')
} else if (mounth <= 0 || mounth > 12){
console.log('Please enter a correct value')
} else{
console.log('You chose Atumn season')
}
console.log('------------------------------------------------------------------------------------------------------')
// 7. Muestra el número de días que tiene un mes dependiendo de la variable del ejercicio anterior
console.log('Exercise 7')
let actualYear = 2024
if (mounth <=0 || mounth >12){
console.log('Please enter a correct value')
} else if(mounth == 2){
if(actualYear % 4 === 0){
console.log(`You chose February and we are in a leap year (${actualYear}), therefore February has 29 days`)
} else {
console.log(`You chose February, we are not in a leap year, therefore February has 28 days`)
}
} else if (mounth == 4 || mounth == 6 || mounth == 9 || mounth == 11){
console.log('You chose between april, june september or november mounths, each of this mounths has 30 days')
} else{
console.log('You chose between january, march, july, august, october or december mounths, each of this mounths has 31 days')
}
console.log('----------------------------------------------------------------------------------------------------------')
// switch
console.log('Switch Exercises')
// 8. Usa un switch para imprimir un mensaje de saludo diferente dependiendo del idioma
console.log('Excersise 8')
let idiom = 'dutch'
let theMessage
switch (idiom){
case 'spanish':
theMessage = 'Hola a todos y todas!'
break
case 'english':
theMessage = 'Hi everyone!!'
break
case 'dutch':
theMessage = 'Hallo allemaal!'
break
case 'french':
theMessage = 'Bonjour à tous !'
break
case 'japanese':
theMessage = '皆さん、こんにちは'
break
default:
if(idiom != typeof String){
theMessage = 'Please enter a correct value'
}
}
console.log(theMessage)
console.log('----------------------------------------------------------------------------------------------')
// 9. Usa un switch para hacer de nuevo el ejercicio 6
console.log('Exercise 9')
let youMounth = 4
let theSeasonIs
switch(youMounth){
case 1:
theSeasonIs = 'The season is Winter'
break
case 2:
theSeasonIs = 'The season is Winter'
break
case 3:
theSeasonIs = 'The season is Winter'
break
case 4:
theSeasonIs = 'The season is Spring'
break
case 5:
theSeasonIs = 'The season is Spring'
break
case 6:
theSeasonIs = 'The season is Spring'
break
case 7:
theSeasonIs = 'You chose Summer season'
break
case 8:
theSeasonIs = 'You chose Summer season'
break
case 9:
theSeasonIs = 'You chose Summer season'
break
case 10:
theSeasonIs = 'You chose Summer Autumn'
break
case 11:
theSeasonIs = 'You chose Summer Autumn'
break
case 12:
theSeasonIs = 'You chose Summer Autumn'
break
default:
theSeasonIs = 'Please enter a correct value'
}
console.log(theSeasonIs)
console.log('-----------------------------------------------------------------------------------------------------')
// 10. Usa un switch para hacer de nuevo el ejercicio 7
console.log('exercise 10')
let thisMounth = 10
let days
let thisYear = 2024
switch(thisMounth){
case 1:
days = 'January has 31 days'
break
case 2:
if(thisYear % 4 === 0){
days = `You chose February and we are in a leap year (${thisYear}), therefore February has 29 days`
} else {
days = `You chose February, we are not in a leap year, therefore February has 28 days`
}
break
case 3:
days = 'March has 31 days'
break
case 4:
days = 'april has 30 days'
break
case 5:
days = 'May has 31 days'
break
case 6:
days = 'june has 30 days'
break
case 7:
days = 'July has 31 days'
break
case 8:
days = 'August has 31 days'
break
case 9:
days = 'September has 30 days'
break
case 10:
days = 'October has 31 days'
break
case 11:
days = 'November has 30 days'
break
case 12:
days = 'December has 31 days'
break
default:
days = 'Please enter a correct value'
}
console.log(days)