This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Dado un arreglo de números positivos, encontrar el elemento más pequeño y retornar su índice. | |
| //empieza javascript | |
| //ARREGLO de Números | |
| var arr = [3, 2, 1]; | |
| // encontrar el menor | |
| var menor = Math.min(...arr); | |
| //console.log(menor); encuentro el numero menor | |
| // encuentra el correcto | |
| function encontrar(numero) { | |
| return numero === menor; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Dado un arr y un number, retornar el indice de number | |
| //empieza javascript | |
| //ARREGLO de Números | |
| var arr = [1, 5, 8, 19]; | |
| // numero a comparar | |
| var number = 8; | |
| // encuentra el correcto | |
| function encontrar(numero) { | |
| return numero === number; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Dados dos arreglos, devolver un nuevo arreglo que contenga la suma de los índices de cada array, | |
| //empieza javascript | |
| //ARREGLOS de Números | |
| var arr1 = [1, 2, 3, 4, 10]; | |
| var arr2 = [5, 6, 7, 8, 35]; | |
| //ARREGLO contenedor final | |
| var res = []; | |
| //recorrer todos los elementos | |
| for (i = 0; i < arr1.length; i++) { | |
| res[i] = arr1[i] + arr2[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Dado un arreglo de 10 números, retornar un nuevo arreglo solo con los números pares. | |
| //empieza javascript | |
| //ARREGLO de Números | |
| var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
| //ARREGLO contenedor final | |
| var res = []; | |
| // recorrer todos los elementos | |
| arr.forEach(function(pares) { | |
| // formula para saber si son números pares | |
| if (pares % 2 === 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Dado un arreglo de 4 números positivos, devolver un nuevo arreglo con cada elemento multiplicado por 12. | |
| //empieza javascript | |
| //ARREGLO DE Números Positivos | |
| var arrPositivos = [1, 2, 3, 4]; | |
| // Multiplo a utilizar | |
| var x = 12; | |
| //ARREGLO contenedor final | |
| var res = []; | |
| // recorrer todos los elementos | |
| arrPositivos.forEach(function(multiplo) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //factorial de un número | |
| var factorial = x => { | |
| if (x<=1) return 1; | |
| //console.log(x); | |
| return x* factorial(x-1); | |
| } | |
| console.log(factorial(8)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //división por restas sucesivas | |
| var division = (x, y) => { | |
| if(y > x) { | |
| return 0; | |
| } else { | |
| //console.log(x); | |
| //console.log(y); | |
| return division(x-y, y) + 1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //permita sumar los dígitos de un número | |
| var sum = x => { | |
| if(0<x) { | |
| console.log(x); | |
| // máximo entero menor o igual a un número. | |
| return (x%10) + sum(Math.floor(x/10)); | |
| } | |
| return x; | |
| } | |
| console.log(sum(123)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //multiplicar los elementos de un array | |
| var mult = x => { | |
| //array vacío, la función devuelve uno (sino no se puede realizar el ejercicio) | |
| if (x.length === 0) { | |
| return 1; | |
| } else { | |
| // ES6 asignación por desestructuración | |
| const [first, ...rest] = x; | |
| console.log(x); | |
| return first * mult(rest); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //sumar los elementos de un arreglo | |
| var sum = x=> { | |
| //array vacío, la función devuelve un cero | |
| if (x.length === 0) { | |
| return 0; | |
| } else { | |
| // ES6 asignación por desestructuración | |
| const [first, ...rest] = x; | |
| console.log(x); | |
| return first + sum(rest); |
OlderNewer