|
| 1 | +//Variable global |
| 2 | +const usuGlobal = "ocram1304" |
| 3 | +let fecha = "20/04/2024"; |
| 4 | +//función sencilla |
| 5 | +//funciones que no retornan |
| 6 | +function mifunction(){ |
| 7 | + console.log("hello world"); |
| 8 | +} |
| 9 | +mifunction(); |
| 10 | +//función con parámetros |
| 11 | +function mifunctionV2(author, date){ |
| 12 | + console.log(`hola soy ${author} saludos desde el dia ${date}`); |
| 13 | +} |
| 14 | +mifunctionV2(usuGlobal, fecha); |
| 15 | + |
| 16 | +function actualizarFecha(nFecha){ |
| 17 | + fecha = nFecha; |
| 18 | + |
| 19 | +} |
| 20 | +//funcion con retorno |
| 21 | +function FunRetorno(num1, num2){ |
| 22 | + |
| 23 | + return num1 * num2; |
| 24 | +} |
| 25 | +console.log(FunRetorno(2,3)); |
| 26 | + |
| 27 | +//funcióne anónima |
| 28 | +const cuadrado = function(){ |
| 29 | + return "función anónima"; |
| 30 | +} |
| 31 | +console.log(cuadrado); |
| 32 | +//función flecha |
| 33 | +const pares = [1,2,3,4]; |
| 34 | +const pares2= pares.map((par)=>{ |
| 35 | + return par*2; |
| 36 | +}); |
| 37 | +console.log(pares2); |
| 38 | + |
| 39 | +//JS si soporta crear funciones dentro de funciones |
| 40 | +function funcionAnidada(numero){ |
| 41 | +//Ambito de varibles: las variables declaradas dentro de una función no pueden ser accedidas desde fuera |
| 42 | +//su ámbito es local a la función en donde fueron declaradas, en este ejemplo "num" no puede ser accedido |
| 43 | +//fuera delámbito de "iPrimo" aunque está función este dentro de otra función (funcionAnidada). |
| 44 | + for(let i=0; i<=numero;i++){ |
| 45 | + if(isPrimo(i)){ |
| 46 | + console.log(i); |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + function isPrimo(num){ |
| 52 | + if(num<=1){ |
| 53 | + return false; |
| 54 | + } |
| 55 | + for(let i =2;i<num;i++){ |
| 56 | + if (num % i===0){ |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + return true; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | +funcionAnidada(20); |
| 67 | + |
| 68 | +//Dificultad extra |
| 69 | +function difExtra(para1,para2){ |
| 70 | + let onlyNumber = 0; |
| 71 | + for(let i= 0; i<=100;i++){ |
| 72 | + |
| 73 | + |
| 74 | + if(i%5===3){ |
| 75 | + console.log(para1); |
| 76 | + } |
| 77 | + else if(i%5===0){ |
| 78 | + console.log(para2) |
| 79 | + } |
| 80 | + else if(i%3===0 && i%5===0){ |
| 81 | + console.log(`${para1}, ${para2} `); |
| 82 | + } |
| 83 | + else{ |
| 84 | + console.log(i); |
| 85 | + onlyNumber++; |
| 86 | + } |
| 87 | + } |
| 88 | + console.log("Numeros en lugar de texto", onlyNumber); |
| 89 | +} |
| 90 | +difExtra(multiplo3,multiplo5); |
0 commit comments