Skip to content

Commit

Permalink
Merge pull request #1654 from juandaherrera/main
Browse files Browse the repository at this point in the history
#5 - Javascript
  • Loading branch information
kontroldev authored Feb 11, 2024
2 parents fa8a0b2 + 45561f0 commit 8b1489a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Roadmap/05 - VALOR Y REFERENCIA/javascript/juandaherrera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Por valor (inmutables)
let number = 42;
let string = "Hola mundo!";
let boolean = true;

// Por referencia (mutables)
let myList = [1, 2, 3];
let myDict = { Nombre: "Juan", Apellido: "Herrera" };

// Función con parámetros "por valor"
function elevateValue(value, n) {
value **= n;
console.log(value);
}

console.log(number);
elevateValue(number, 2);
console.log(number);

// Función con parámetros "por referencia"
function elevateValues(container, n) {
for (let index = 0; index < container.length; index++) {
container[index] = container[index] ** n;
}
console.log(container);
}

console.log(myList);
elevateValues(myList, 2);
console.log(myList);

console.log("-----------------------------------------");

/*
* DIFICULTAD EXTRA (opcional):
*/

function areValuesEqual(original1, original2, new1, new2) {
return original1 === new1 && original2 === new2;
}

function swapByValue(param1, param2) {
let temp = param1;
param1 = param2;
param2 = temp;
return [param1, param2];
}

let value1 = "Juan";
let value2 = "David";

console.log(value1, value2);
let [value3, value4] = swapByValue(value1, value2);
console.log(value1, value2);
console.log(value3, value4);
console.log(
"El valor de las variables originales fue invertido:",
areValuesEqual(value1, value2, value3, value4)
);
console.log("---------------------------");

function swapByReference(param1, param2) {
if (param1.length === param2.length) {
for (let index = 0; index < param1.length; index++) {
let temp = param1[index];
param1[index] = param2[index];
param2[index] = temp;
}
}
return [param1, param2];
}

value5 = ["Juan", "David"];
value6 = ["Herrera", "Parra"];

console.log(value5, value6);
let [value7, value8] = swapByReference(value5, value6);
console.log(value5, value6);
console.log(value7, value8);
console.log(
"El valor de las variables originales fue invertido:",
areValuesEqual(value5, value6, value7, value8)
);

0 comments on commit 8b1489a

Please sign in to comment.