|
| 1 | +// Function without parameters and return value |
| 2 | +function sayHello() { |
| 3 | + console.log("Hello!"); |
| 4 | +} |
| 5 | + |
| 6 | +sayHello(); |
| 7 | + |
| 8 | +// Function with parameters and return value |
| 9 | +function addNumbers(a, b) { |
| 10 | + return a + b; |
| 11 | +} |
| 12 | + |
| 13 | +console.log(addNumbers(23, 32)); |
| 14 | + |
| 15 | +// Function within a function |
| 16 | +function outerFunction() { |
| 17 | + console.log("Outer function"); |
| 18 | + |
| 19 | + function innerFunction() { |
| 20 | + console.log("Inner function"); |
| 21 | + } |
| 22 | + |
| 23 | + innerFunction(); |
| 24 | +} |
| 25 | + |
| 26 | +outerFunction(); |
| 27 | + |
| 28 | +// Using built-in functions |
| 29 | +console.log(Math.random()); |
| 30 | + |
| 31 | +// Local and global variables |
| 32 | +function localAndGlobal() { |
| 33 | + var localVariable = "I am a local variable"; |
| 34 | + globalVariable = "I am a global variable"; |
| 35 | + |
| 36 | + console.log(localVariable); |
| 37 | + console.log(globalVariable); |
| 38 | +} |
| 39 | + |
| 40 | +localAndGlobal(); |
| 41 | +console.log(globalVariable); |
| 42 | + |
| 43 | +// Arrow functions |
| 44 | + |
| 45 | +// Function without parameters and return value |
| 46 | +const sayHello2 = () => console.log("Hello!"); |
| 47 | +sayHello2(); |
| 48 | + |
| 49 | +// Function with parameters and return value |
| 50 | +const addNumbers2 = (a, b) => a + b; |
| 51 | +console.log(addNumbers2(23, 32)); |
| 52 | + |
| 53 | +// Function within a function |
| 54 | +const outerFunction2 = () => { |
| 55 | + console.log("Outer function"); |
| 56 | + |
| 57 | + const innerFunction2 = () => console.log("Inner function"); |
| 58 | + |
| 59 | + innerFunction2(); |
| 60 | +}; |
| 61 | + |
| 62 | +outerFunction2(); |
| 63 | + |
| 64 | +// Local and global variables |
| 65 | +const localAndGlobal2 = () => { |
| 66 | + const localVariable2 = "I am a local variable"; |
| 67 | + globalVariable2 = "I am a global variable"; |
| 68 | + |
| 69 | + console.log(localVariable2); |
| 70 | + console.log(globalVariable2); |
| 71 | +}; |
| 72 | + |
| 73 | +localAndGlobal2(); |
| 74 | +console.log(globalVariable2); |
| 75 | + |
| 76 | +//Dificultad Extra |
| 77 | +function imprimirNumerosYTextos(cadena1, cadena2) { |
| 78 | + let contador = 0; |
| 79 | + for (let i = 1; i <= 100; i++) { |
| 80 | + if (i % 3 === 0 && i % 5 === 0) { |
| 81 | + console.log(cadena1 + " " + cadena2); |
| 82 | + } else if (i % 3 === 0) { |
| 83 | + console.log(cadena1); |
| 84 | + } else if (i % 5 === 0) { |
| 85 | + console.log(cadena2); |
| 86 | + } else { |
| 87 | + console.log(i); |
| 88 | + contador++; |
| 89 | + } |
| 90 | + } |
| 91 | + return "\nCantidad de veces que se imprimio numeros: " + contador; |
| 92 | +} |
| 93 | + |
| 94 | +console.log(imprimirNumerosYTextos("Ludwig", "Wolfgang")); |
0 commit comments