Skip to content

Commit 4bb11d3

Browse files
Jesusway69Gallitofast
authored andcommitted
mouredev#2 - Javascript
1 parent 2cf7f65 commit 4bb11d3

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
console.clear()
2+
/*
3+
* EJERCICIO:
4+
* - Crea ejemplos de funciones básicas que representen las diferentes
5+
* posibilidades del lenguaje:
6+
* Sin parámetros ni retorno, con uno o varios parámetros, con retorno...
7+
* - Comprueba si puedes crear funciones dentro de funciones.
8+
* - Utiliza algún ejemplo de funciones ya creadas en el lenguaje.
9+
* - Pon a prueba el concepto de variable LOCAL y GLOBAL.
10+
* - Debes hacer print por consola del resultado de todos los ejemplos.
11+
* (y tener en cuenta que cada lenguaje puede poseer más o menos posibilidades)
12+
*
13+
* Presta especial atención a la sintaxis que debes utilizar en cada uno de los casos.
14+
* Cada lenguaje sigue una convenciones que debes de respetar para que el código se entienda.
15+
*/
16+
17+
// VARIABLES GLOBALES
18+
var acc = 0
19+
var operation = ''
20+
var globalVariable = "Soy una variable global"
21+
var string1 = "Hola"
22+
var string2 = "Javascript"
23+
24+
//FUNCIÓN SUMA SIMPLE SIN RETORNO
25+
function sumPrint(a, b) {
26+
console.log(`${a} + ${b} = ${a + b} \n`)
27+
}
28+
sumPrint(10, 9)
29+
30+
// FUNCIÓN SUMA SIMPLE CON RETORNO
31+
function sumReturn(a, b) {
32+
return `${a} + ${b} = ` + (a + b)
33+
}
34+
console.log(sumReturn(9, 8), '\n')
35+
36+
// FUNCIÓN ANÓNIMA DE SUMA SIMPLE
37+
let sumAnonymousFuntcion = function (a, b) {
38+
let operation = `${a} + ${b} = `
39+
return operation + (a + b)
40+
}
41+
console.log(sumAnonymousFuntcion(10, 20), '\n')
42+
43+
//FUNCIÓN FLECHA DE SUMA SIMPLE
44+
let sumArrowFuntcion = (a, b) => `${a} + ${b} = ` + (a + b)
45+
console.log(sumArrowFuntcion(50, 50), '\n')
46+
47+
// FUNCIÓN SUMA CON OPERANDOS VARIABLES POR PROPAGACIÓN CON RETORNO
48+
function sumSpreadingReturn(...numbers) {
49+
acc = 0
50+
operation = ''
51+
for (const [index, number] of numbers.entries()) {
52+
acc += number
53+
if (index < numbers.length - 1) operation += number + ' + '
54+
else operation += number + ' = '
55+
}
56+
return operation + acc
57+
}
58+
console.log(sumSpreadingReturn(7, 8, 3, 14, 25), '\n')
59+
console.log(sumSpreadingReturn(8, 9, 11, 50, 14, 8, 71, 63, 2, 32), '\n')
60+
61+
// FUNCIÓN SUMA CON OPERANDOS VARIABLES POR PROPAGACIÓN SIN RETORNO
62+
function sumSpreadingPrint(...numbers) {
63+
acc = 0
64+
operation = ''
65+
for (let i = 0; i < numbers.length; i++) {
66+
acc += numbers[i]
67+
if (i < numbers.length - 1) operation += numbers[i] + ' + '
68+
else operation += numbers[i] + ' ='
69+
}
70+
console.log(operation, acc, '\n')
71+
}
72+
sumSpreadingPrint(8, 7, 1, 16, 52, 9, 13, 4)
73+
sumSpreadingPrint(5, 1, 7, 0.3, 10, 4)
74+
sumSpreadingPrint(5, 8, 100, 50)
75+
76+
// FUNCIÓN ANÓNIMA SUMA CON OPERANDOS VARIABLES POR PROPAGACIÓN CON RETORNO
77+
const anonymousFunction = function (...numbers) {
78+
acc = 0
79+
operation = ''
80+
for (const [index, number] of numbers.entries()) {
81+
acc += number
82+
if (index < numbers.length - 1) {
83+
operation += number + ' + '
84+
} else {
85+
operation += number + ' = '
86+
}
87+
88+
}
89+
return operation + acc
90+
}
91+
console.log(anonymousFunction(4, 48, 7, 9, -10, 2, 1, 20), '\n')
92+
console.log(anonymousFunction(3.56, 4.88, 9.48, 1.22, 5.71, 3.24, 6.91), '\n')
93+
94+
// FUNCIÓN FLECHA SUMA CON OPERANDOS VARIABLES POR PROPAGACIÓN CON RETORNO
95+
const anonymousArrowFunction = (...numbers) => {
96+
acc = 0
97+
operation = ''
98+
for (const [index, number] of numbers.entries()) {
99+
acc += number
100+
if (index < numbers.length - 1) {
101+
operation += number + ' + '
102+
} else {
103+
operation += number + ' = '
104+
}
105+
106+
}
107+
return operation + acc
108+
}
109+
console.log(anonymousArrowFunction(1, 7, 8), '\n')
110+
console.log(anonymousArrowFunction(47, 72, 98), '\n')
111+
112+
// FUNCIÓN QUE IMPRIME UNA VARIABLE LOCAL DECLARADA DENTRO Y OTRA GLOBAL DECLARADA FUERA
113+
let printVariables = function () {
114+
let localVariable = "Soy una variable local"
115+
console.log(localVariable)
116+
console.log(globalVariable)
117+
}
118+
printVariables()
119+
120+
// FUNCIÓN ANÓNIMA QUE SE LE PASA COMO ARGUMENTO A UNA FUNCIÓN DE ORDEN SUPERIOR SOBRE UN LISTADO
121+
// Y DEVUELVE OTRO LISTADO CON LOS NÚMEROS TRANSFORMADOS EN BASE 2 USANDO LOS ORIGINALES COMO EXPONENTES
122+
const simpleNumbers = [0, 1, 2, 3, 4, 5, 6, 7]
123+
const base2Value = simpleNumbers.map(function (num) {
124+
return 2 ** num
125+
})
126+
console.log('\n', base2Value, '\n')
127+
128+
// FUNCIÓN FLECHA QUE RECIBE UN NÚMERO Y UTILIZA INTERNAMENTE LA FUNCIÓN ANTERIOR PARA
129+
// DEVOLVER EL NÚMERO BINARIO DEL NÚMERO RECIBIDO
130+
let decToBin = (num) => {
131+
bin = ''
132+
for (let i = base2Value.length - 1; i >= 0; i--) {
133+
if (num < base2Value[i]) {
134+
bin += 0 + ' '
135+
} else {
136+
bin += 1 + ' '
137+
num -= base2Value[i]
138+
}
139+
}
140+
return bin
141+
}
142+
console.log(decToBin(100), '\n')
143+
144+
/*
145+
* DIFICULTAD EXTRA (opcional):
146+
* Crea una función que reciba dos parámetros de tipo cadena de texto y retorne un número.
147+
* - La función imprime todos los números del 1 al 100. Teniendo en cuenta que:
148+
* - Si el número es múltiplo de 3, muestra la cadena de texto del primer parámetro.
149+
* - Si el número es múltiplo de 5, muestra la cadena de texto del segundo parámetro.
150+
* - Si el número es múltiplo de 3 y de 5, muestra las dos cadenas de texto concatenadas.
151+
* - La función retorna el número de veces que se ha impreso el número en lugar de los textos.
152+
*/
153+
154+
function numbers(str1, str2) {
155+
let counter = 0
156+
for (let i = 1; i <= 100; i++) {
157+
if (i % 3 == 0 && i % 5 == 0) console.log(str1, str2);
158+
else if (i % 3 == 0) console.log(str1);
159+
else if (i % 5 == 0) console.log(str2);
160+
else {
161+
console.log(i)
162+
counter++
163+
}
164+
}
165+
console.log(`\nLos números se han impreso ${counter} veces \n`)
166+
}
167+
numbers(string1, string2)

0 commit comments

Comments
 (0)