Skip to content

Commit 9964d01

Browse files
authored
Merge pull request mouredev#6367 from duendeintemporal/main
#5 - javascript
2 parents 6347cd7 + 2eef7af commit 9964d01

File tree

2 files changed

+248
-1
lines changed

2 files changed

+248
-1
lines changed

Roadmap/04 - CADENAS DE CARACTERES/javascript/duendeintemporal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ window.addEventListener('load', function(){
146146
body.style.setProperty('background', '#000');
147147
body.style.setProperty('text-align', 'center');
148148

149-
title.textContent = 'Retosparaprogramadores #3.';
149+
title.textContent = 'Retosparaprogramadores #4.';
150150
title.style.setProperty('font-size', '3.5vmax');
151151
title.style.setProperty('color', '#fff');
152152
title.style.setProperty('line-height', '100vh');
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
//Exercice #5 Valor y Referencia
2+
// I use GPT for translation. generate comments an also as a reference.
3+
4+
let log = console.log.bind(console);
5+
6+
//Variables by Value
7+
8+
/*Primitive data types in JavaScript (such as number, string, boolean, null, undefined, and symbol) are assigned by value. This means that when you assign a primitive value to a variable, a copy of that value is made.*/
9+
10+
let a = 44; // a is a number (primitive type)
11+
let b = a; // b is assigned the value of a
12+
13+
log(a); // 44
14+
log(b); // 44
15+
16+
b += 20; // Changing b does not affect a
17+
log(a); // 44
18+
log(b); // 64
19+
20+
//Variables by Reference
21+
22+
/*Reference data types in JavaScript (such as object, array, and function) are assigned by reference. This means that when you assign an object to a variable, you are actually assigning a reference to that object, not a copy of it.*/
23+
24+
25+
let obj1 = { name: "Julia" }; // obj1 is an object (reference type)
26+
let obj2 = obj1; // obj2 is assigned the reference of obj1
27+
28+
log(obj1.name); // Julia
29+
log(obj2.name); // Julia
30+
31+
obj2.name = "Karla"; // Changing obj2 affects obj1 because they reference the same object
32+
log(obj1.name); // Karla
33+
log(obj2.name); // Karla
34+
35+
obj2.age = 32;
36+
log(obj2.age)// 32
37+
log(obj1.age)// 32
38+
39+
//Arrays (Reference Type) in JavaScript are also reference types.
40+
41+
let arr1 = [7, 4, 90]; // arr1 is an array (reference type)
42+
let arr2 = arr1; // arr2 is assigned the reference of arr1
43+
44+
log(arr1); // [7, 4, 90]
45+
log(arr2); // [7, 4, 90]
46+
47+
arr2.push(42); // Modifying arr2 affects arr1
48+
log(arr1); // [7, 4, 90, 42]
49+
log(arr2); // [7, 4, 90, 42]
50+
51+
/* By Value: Primitive types (e.g., number, string) are copied when assigned to a new variable.
52+
By Reference: Reference types (e.g., object, array) are assigned by reference, meaning changes to one variable affect the other if they reference the same object. */
53+
54+
/* In JavaScript, functions are also treated as first-class objects, which means they can be assigned to variables, passed as arguments, and returned from other functions. When you assign a function to a variable, you are assigning a reference to that function, not a copy of it. Here are some examples to illustrate how functions work with reference: */
55+
56+
//Functions Assigned by Reference
57+
58+
//When you assign a function to a new variable, both variables point to the same function in memory.
59+
60+
function greet() {
61+
log("Hello everybody! is time for coding...");
62+
}
63+
64+
let greetCopy = greet; // greetCopy is assigned the reference of greet
65+
66+
greet(); // Hello everybody! is time for coding...
67+
greetCopy(); // Hello everybody! is time for coding...
68+
69+
// Changing the function reference
70+
greetCopy = function() {
71+
log("This world is turning into a weird scenario with all those AIs, M2M (technology), and there are cyber attacks that can explode beepers, cell phones, and a big media that shows only a tiny fragment of reality. ");
72+
};
73+
74+
greet(); // Hello everybody! is time for coding...
75+
greetCopy(); //This world is turning into a weird scenario with all those AIs, M2M (technology), and there are cyber attacks that can explode beepers, cell phones, and big media that shows only a tiny fragment of reality.
76+
77+
// Passing Functions as Arguments
78+
79+
//You can pass functions as arguments to other functions. This demonstrates that functions are treated as reference types.
80+
81+
82+
function callFunct(fn) {
83+
fn();
84+
}
85+
86+
function sayByeLady() {
87+
log("See you later, lady!");
88+
}
89+
90+
callFunct(sayByeLady); // See you later, lady
91+
92+
//Returning Functions from Functions
93+
94+
//You can also return functions from other functions, which shows that functions can be created dynamically and assigned by reference.
95+
96+
97+
function saySomethingTo(greeting) {
98+
return function(name) {
99+
log(`${greeting}, ${name}!`);
100+
};
101+
}
102+
103+
let say = saySomethingTo("Wellcome to coding lab");
104+
let say2 = saySomethingTo("Is a long way home");
105+
106+
say("Nicky"); // Wellcome to coding lab, Nicky
107+
say2("Jhon"); // Is a long way home, Jhon
108+
109+
// Modifying Functions
110+
111+
//Since functions are reference types, if you modify a function that is referenced by multiple variables, it will affect all references.
112+
113+
function hiMiss() {
114+
log("Hello, miss. Have a beautiful day");
115+
}
116+
117+
let funcRef = hiMiss; // funcRef points to hiMiss
118+
119+
funcRef(); // Hello, miss, have a beautiful day
120+
121+
// Modifying the original function
122+
hiMiss = function() {
123+
log("Sometimes I feel jealous of the wind that caresses your hair, sometimes the rain....");
124+
};
125+
126+
funcRef(); // Hello, miss, have a beautiful day (still points to the original function)
127+
128+
//but if we fro example...
129+
funcRef = ()=>{
130+
return hiMiss;
131+
}
132+
133+
funcRef()()// Sometimes I feel jealous of the wind that caresses your hair, sometimes the rain.... (now every change in hiMiss will be reflexted)
134+
135+
136+
/* In JavaScript, when you pass arguments to a function, the behavior depends on whether the argument is a primitive type (passed by value) or a reference type (passed by reference). Here’s an example that demonstrates both concepts: */
137+
138+
139+
// Function that takes a primitive type (passed by value)
140+
function doSomething(value) {
141+
value = 20; // This change does not affect the original variable
142+
log("Value Inside doSomething:", value); // Value Inside doSomething: 20
143+
}
144+
145+
// Function that takes an object (passed by reference)
146+
function setUserName(obj, name) {
147+
obj.name = name; // This change affects the original object
148+
log("Value Inside setUserName:", obj.name);
149+
}
150+
151+
// Testing the functions
152+
let num = 'something';
153+
log("Value Before doSomething:", num); // Value Before doSomething: something
154+
doSomething(num);
155+
log("After doSomething:", num); // After doSomething: something (remains unchanged)
156+
157+
let user = { name: "Luna", age: 32 };
158+
log("Before setUserName:", user.name); // Before setUserName: Luna
159+
setUserName(user, 'Kia'); // Value Inside setUserName: Kia
160+
log("After setUserName:", user.name); // After setUserName: Kia (changed)
161+
162+
//Note: the same apply to arrays that are reference values.
163+
164+
window.addEventListener('load', ()=>{
165+
const body = document.querySelector('body');
166+
const title = document.createElement('h1');
167+
168+
body.style.setProperty('background', '#000');
169+
body.style.setProperty('text-align', 'center');
170+
171+
title.textContent = 'Retosparaprogramadores #5.';
172+
title.style.setProperty('font-size', '3.5vmax');
173+
title.style.setProperty('color', '#fff');
174+
title.style.setProperty('line-height', '100vh');
175+
176+
body.appendChild(title);
177+
178+
setTimeout(()=>{
179+
alert('Retosparaprogramadores #5. Please open the Browser Developer Tools.');
180+
}, 2000);
181+
log( 'Retosparaprogramadores #5'); // this will be logged at the end cause the nature of window event
182+
});
183+
184+
185+
/* EXTRA DIFFICULTY (optional):
186+
187+
Create two programs that receive two parameters (each) defined as variables previously.
188+
One program receives, in one case, two parameters by value, and in another case, by reference.
189+
These parameters are swapped between each other internally, returned, and their return values are assigned to two variables different from the originals. Then, print the value of the original variables and the new ones, checking that their values have been inverted in the second set.
190+
Also verify that the original values have been preserved in the first set.
191+
*/
192+
193+
let user1 = {
194+
name: 'Kasperle',
195+
age: 12
196+
}
197+
198+
let user2 = {
199+
name: 'Snoopy',
200+
age: void(0)
201+
}
202+
203+
log('user1: ', user1);// user1: Object { name: "Kasperle", age: 12 }
204+
log('user2: ', user2);// user2: Object { name: "Snoopy", age: undefined }
205+
206+
function changeUser(obj1, obj2) {
207+
const provisional = {};
208+
Object.assign(provisional, obj1);// Object.assign allow a deep copy
209+
Object.assign(obj1, obj2);
210+
Object.assign(obj2, provisional);
211+
// return [obj1, obj2]
212+
// We can return the objects in an array, but it becomes unnecessary because objects are treated as reference values.
213+
// Therefore, the function will affect the original objects directly.
214+
}
215+
216+
changeUser(user1, user2)
217+
218+
log('user1: ', user1);// user1: Object { name: "Snoopy", age: undefined }
219+
log('user2: ', user2);// user2: Object { name: "Kasperle", age: 12 }
220+
221+
function swapValues(a, b) {
222+
return { a: b, b: a }; // using destructuring to return the swapped values as an object
223+
}
224+
225+
//You can call the function and use destructuring to assign the returned values to new variables:
226+
227+
let x = 85;
228+
let y = 17;
229+
230+
log("Before swap: ", x, y);// Before swap: 85 17
231+
232+
// Call the function and destructure the returned object
233+
({ a: d, b: e } = swapValues(x, y));
234+
235+
log("After swap: ", x, y);// After swap: 85 17
236+
log('new variables with values swapped: ', d, e)// new variables with values swapped: 17 85
237+
238+
//Note: destructuring allow us to achive the effect of reference values with primitive values. Say using destructuring we can achive both effects. Conserving the originals values or swapping the values.
239+
log("Before swap: ", x, y);// Before swap: 85 17
240+
({ a: x, b: y } = swapValues(x, y));
241+
242+
log("After swap: ", x, y);// After swap: 17 85
243+
244+
245+
//Note: using destructuring we can achive both effects. Conserving the originals values or swapping the values
246+
247+
//Note2: have in mind that when you use destructuring you make a shallow copy, it means that if are object nested inside, the nested ones don't be copied

0 commit comments

Comments
 (0)