|
| 1 | +/* |
| 2 | + EJERCICIO: |
| 3 | +- Muestra ejemplos de asignación de variables "por valor" y "por referencia", según |
| 4 | +su tipo de dato. |
| 5 | +- Muestra ejemplos de funciones con variables que se les pasan "por valor" y |
| 6 | +"por referencia", y cómo se comportan en cada caso en el momento de ser modificadas. |
| 7 | +(Entender estos conceptos es algo esencial en la gran mayoría de lenguajes) |
| 8 | +*/ |
| 9 | + |
| 10 | +// Assigning a value by value |
| 11 | +let currentYear = 2025; |
| 12 | +console.log('Assigned by value: ' + currentYear); |
| 13 | + |
| 14 | +// Assigning a value by reference |
| 15 | +let animals = ['lion', 'fish', 'dog']; |
| 16 | +console.log('Assigned by value: ' + animals); |
| 17 | +let animalsArrayCopy = animals; |
| 18 | +console.log('Assigned by reference: ' + animalsArrayCopy); |
| 19 | + |
| 20 | +// Changing the first array assigned by value |
| 21 | +animals.push('cat'); |
| 22 | +console.log('Assigned by value after change: ' + animals); |
| 23 | +// The array assigned by reference is also updated |
| 24 | +console.log('Assigned by reference after change: ' + animalsArrayCopy); |
| 25 | + |
| 26 | + |
| 27 | +/* |
| 28 | +* DIFICULTAD EXTRA (opcional): |
| 29 | +Crea dos programas que reciban dos parámetros (cada uno) definidos como variables anteriormente. |
| 30 | +- Cada programa recibe, en un caso, dos parámetros por valor, y en otro caso, por referencia. |
| 31 | +Estos parámetros los intercambia entre ellos en su interior, los retorna, y su retorno |
| 32 | +se asigna a dos variables diferentes a las originales. A continuación, imprime el valor de las |
| 33 | +variables originales y las nuevas, comprobando que se ha invertido su valor en las segundas. |
| 34 | +Comprueba también que se ha conservado el valor original en las primeras. |
| 35 | +*/ |
| 36 | + |
| 37 | +// Receives two values and assign them by value |
| 38 | +function firstProgram(first, second) { |
| 39 | + let aux = first; |
| 40 | + first = second; |
| 41 | + second = aux; |
| 42 | + return [first, second]; |
| 43 | +} |
| 44 | +function secondProgram(first, second) { |
| 45 | + first = vegetables; |
| 46 | + second = fruits; |
| 47 | + return [first, second]; |
| 48 | +} |
| 49 | + |
| 50 | +console.log('---------------------------------'); |
| 51 | +const a = 1; |
| 52 | +const b = 2; |
| 53 | +console.log('Variables before executing first method'); |
| 54 | +console.log('firstVariable = ' + a + ', secondVariable = ' + b); |
| 55 | +console.log('Variables after executing first method'); |
| 56 | +let [firstValue, secondValue] = firstProgram(a, b); |
| 57 | +console.log('firstVariable = ' + firstValue + ', secondVariable = ' + secondValue); |
| 58 | + |
| 59 | +console.log('---------------------------------'); |
| 60 | +let fruits = ['apple', 'banana', 'grape']; |
| 61 | +let vegetables = ['tomato', 'carrot', 'radish']; |
| 62 | +console.log('Variables before executing second method'); |
| 63 | +console.log('firstVariable = ' + fruits + ', secondVariable = ' + vegetables); |
| 64 | +console.log('Variables before executing second method'); |
| 65 | +let [firstArray, secondArray] = secondProgram(fruits, vegetables); |
| 66 | +console.log('firstVariable = ' + firstArray + ', secondVariable = ' + secondArray); |
0 commit comments