-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy path09-conditionals-exercises.js
165 lines (136 loc) · 3.76 KB
/
09-conditionals-exercises.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
/*
Clase 3 en vídeo | 24/07/2024
Condicionales, arrays y sets
https://www.twitch.tv/videos/2206228701?t=00h16m02s
*/
// if/else/else if/ternaria
// 1. Imprime por consola tu nombre si una variable toma su valor
let myName = "Zulangy"
if (myName == "Zulangy"){
console.log(myName)
}
// 2. Imprime por consola un mensaje si el usuario y contraseña coincide con unos establecidos
let user = "Zulangy"
let password = "1234"
if (user == "Zulangy" && password == "1234"){
console.log("Usuario y contraseña correctos")
}else{
console.log("Usuario o contraseña incorrectos")
}
// 3. Verifica si un número es positivo, negativo o cero e imprime un mensaje
let number = 5
if (number > 0){
console.log("El número es positivo")
}else if (number < 0){
console.log("El número es negativo")
}else{
console.log("El número es cero")
}
// 4. Verifica si una persona puede votar o no (mayor o igual a 18) e indica cuántos años le faltan
let age = 17
if (age >= 18){
console.log("Puede votar")
} else {
console.log(`No puede votar, le faltan ${18 - age} años`)
}
// 5. Usa el operador ternario para asignar el valor "adulto" o "menor" a una variable
// dependiendo de la edad
age = 32
let status = age >= 18 ? "adulto" : "menor"
console.log(status)
// 6. Muestra en que estación del año nos encontramos dependiendo del valor de una variable "mes"
let season
let month = "august"
if (month == "january" || month == "february" || month == "march"){
season = "winter"
}else if (month == "april" || month == "may" || month == "june"){
season = "spring"
}else if (month == "july" || month == "august" || month == "september"){
season = "summer"
}else if (month == "october" || month == "november" || month == "december"){
season = "autumn"
}else{
season = "Invalid month"
}
console.log(`In ${month}, We are in ${season}`)
// 7. Muestra el número de días que tiene un mes dependiendo de la variable del ejercicio anterior
let days
month = "february"
if (month == "january" || month == "march" || month == "may" || month == "july" || month == "august" || month == "october" || month == "december"){
days = 31
}else if (month == "april" || month == "june" || month == "september" || month == "november"){
days = 30
}else if (month == "february"){
days = 28
}else{
days = "Invalid month"
}
console.log(`The month of ${month} have ${days} days`)
// switch
// 8. Usa un switch para imprimir un mensaje de saludo diferente dependiendo del idioma
let language = "es"
switch (language) {
case "es":
console.log("Hola")
break
case "en":
console.log("Hello")
break
case "fr":
console.log("Bonjour")
break
default:
console.log("Idioma no soportado")
}
// 9. Usa un switch para hacer de nuevo el ejercicio 6
season
month = "january"
switch (month) {
case "january":
case "february":
case "march":
season = "winter"
break
case "april":
case "may":
case "june":
season = "spring"
break
case "july":
case "august":
case "september":
season = "summer"
break
case "october":
case "november":
case "december":
season = "autumn"
break
default:
season = "Invalid month"
}
console.log(`In ${month}, We are in ${season}`)
// 10. Usa un switch para hacer de nuevo el ejercicio 7
switch (month) {
case "january":
case "march":
case "may":
case "july":
case "august":
case "october":
case "december":
days = 31
break
case "april":
case "june":
case "september":
case "november":
days = 30
break
case "february":
days = 28
break
default:
days = "Invalid month"
}
console.log(`The month of ${month} have ${days} days`)