|
| 1 | +// Función sin parámetros ni retorno |
| 2 | +function sayHello() { |
| 3 | + console.log("Hello!"); |
| 4 | +} |
| 5 | + |
| 6 | +sayHello(); |
| 7 | + |
| 8 | +// Función con un parámetro y retorno |
| 9 | +function myNameIs(name) { |
| 10 | + return "My name is " + name; |
| 11 | +} |
| 12 | + |
| 13 | +console.log(myNameIs("Raquel Tejada")); |
| 14 | + |
| 15 | +// Función con múltiples parámetros y retorno |
| 16 | +function sayFullName(name, surname) { |
| 17 | + return name + " " + surname; |
| 18 | +} |
| 19 | + |
| 20 | +console.log(sayFullName("Raquel", "Tejada")); |
| 21 | + |
| 22 | +// Función expresión |
| 23 | +const myAge = function (age) { |
| 24 | + return "this is my age" + age; |
| 25 | +}; |
| 26 | + |
| 27 | +// Función flecha |
| 28 | +const arrowFunction = () => console.log("this is arrow function"); |
| 29 | + |
| 30 | +// Función anidada |
| 31 | +function parent(parentName) { |
| 32 | + return function child(childName) { |
| 33 | + return `${parentName} is the father of ${childName}`; |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +// Variable global y variable local |
| 38 | +const globalVariable = "Esto es una variable global"; |
| 39 | + |
| 40 | +function localVariable() { |
| 41 | + const localVariable = "Esto es una variable local"; |
| 42 | + console.log(globalVariable); |
| 43 | + console.log(localVariable); |
| 44 | +} |
| 45 | + |
| 46 | +localVariable(); |
| 47 | + |
| 48 | +// Dificultad extra |
| 49 | +function numbers(chainNumber1, chainNumber2) { |
| 50 | + for (let i = 1; i <= 100; i++) { |
| 51 | + i % 3 === 0 && i % 5 === 0 |
| 52 | + ? console.log(chainNumber1 + chainNumber2) |
| 53 | + : i % 3 === 0 |
| 54 | + ? console.log(chainNumber1) |
| 55 | + : i % 5 === 0 |
| 56 | + ? console.log(chainNumber2) |
| 57 | + : console.log(i); |
| 58 | + } |
| 59 | +} |
| 60 | +numbers("trying", "extra mile"); |
0 commit comments