Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions problems/scope/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,63 @@ Functions defined inside other functions, known as nested functions, have access
Pay attention to the comments in the code below:

```js
var a = 4; // a is a global variable, it can be accessed by the functions below
const a = 4; // a is a global variable, it can be accessed by the functions below

function foo() {
var b = a * 3; // b cannot be accessed outside foo function, but can be accessed by functions
// defined inside foo
function bar(c) {
var b = 2; // another `b` variable is created inside bar function scope
// the changes to this new `b` variable don't affect the old `b` variable
console.log( a, b, c );
}

bar(b * 4);
let b = a * 3; // b cannot be accessed outside foo function, but can be accessed by functions
// defined inside foo
function bar(c) {
let b = 2; // another `b` variable is created inside bar function scope
// the changes to this new `b` variable don't affect the old `b` variable
console.log( a, b, c );
}

bar(b * 4);
}

foo(); // 4, 2, 48
```


IIFE, Immediately Invoked Function Expression, is a common pattern for creating local scopes.

Example:
```js
(function(){ // the function expression is surrounded by parenthesis
(function(){ // the function expression is surrounded by parenthesis
// variables defined here
// can't be accessed outside
})(); // the function is immediately invoked
})(); // the function is immediately invoked
```
## The challenge:

Create a file named `scope.js`.

In that file, copy the following code:
```js
var a = 1, b = 2, c = 3;
let a = 1, b = 2, c = 3;

(function firstFunction(){
var b = 5, c = 6;
let b = 5, c = 6;

(function secondFunction(){
var b = 8;
(function secondFunction(){
let b = 8;

(function thirdFunction(){
var a = 7, c = 9;
(function thirdFunction(){
let a = 7, c = 9;

(function fourthFunction(){
var a = 1, c = 8;
(function fourthFunction(){
let a = 1, c = 8;

})();
})();
})();
})();
})();
})();
})();
```

Use your knowledge of the variables' `scope` and place the following code inside one of the functions in `scope.js`
so the output is `a: 1, b: 8, c: 6`
```js
console.log("a: " + a + ", b: " + b + ", c: " + c);
console.log(`a: ${a}, b: ${b}, c: ${c}`);
```

Check to see if your program is correct by running this command:
Expand Down
49 changes: 24 additions & 25 deletions problems/scope/problem_es.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,58 @@ Las funciones definidas dentro de otras funciones, conocidas como funciones anid
Presta atención a los comentarios en el siguiente código:

```js
var a = 4; // es una variable global, puede ser accedida por las siguientes funciones
const a = 4; // es una variable global, puede ser accedida por las siguientes funciones

function foo() {
var b = a * 3; // b no puede ser accedida por fuera de la función foo, pero puede ser accedida
// por las funciones definidas dentro de foo
function bar(c) {
var b = 2; // otra variable `b` es creada dentro del ámbito de la función bar
// los cambios a esta nueva `b` no afectan a la vieja variable `b`
console.log( a, b, c );
}

bar(b * 4);
let b = a * 3; // b no puede ser accedida por fuera de la función foo, pero puede ser accedida
// por las funciones definidas dentro de foo
function bar(c) {
let b = 2; // otra variable `b` es creada dentro del ámbito de la función bar
// los cambios a esta nueva `b` no afectan a la vieja variable `b`
console.log( a, b, c );
}

bar(b * 4);
}

foo(); // 4, 2, 48
```
IIFE, Immediately Invoked Function Expression( Expresión de Función Invocada Inmediatamente ), es un patrón común para crear ámbitos locales.
Por ejemplo:
```js
(function(){ // La expresión de la función está entre paréntesis
(function(){ // La expresión de la función está entre paréntesis
// las variables definidas aquí
// no pueden ser accedidas por fuera
})(); // la función es inmediatamente invocada
})(); // la función es inmediatamente invocada
```
## El ejercicio:

Crea un archivo llamado `scope.js`.

En ese archivo, copia el siguiente código:
```js
var a = 1, b = 2, c = 3;
let a = 1, b = 2, c = 3;

(function firstFunction(){
var b = 5, c = 6;
let b = 5, c = 6;

(function secondFunction(){
var b = 8;
(function secondFunction(){
let b = 8;

(function thirdFunction(){
var a = 7, c = 9;
(function thirdFunction(){
let a = 7, c = 9;

(function fourthFunction(){
var a = 1, c = 8;
(function fourthFunction(){
let a = 1, c = 8;

})();
})();
})();
})();
})();
})();
})();
```

Usa tu conocimiento sobre el ámbito de las variables y ubica el siguiente código dentro de alguna de las funciones
en `scope.js` para que la salida sea `a: 1, b: 8, c: 6`
```js
console.log("a: "+a+", b: "+b+", c: "+c);
console.log(`a: ${a}, b: ${b}, c: ${c}`);
```

51 changes: 26 additions & 25 deletions problems/scope/problem_fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ Les fonctions définies à l'intérieur d'autres fonctions, aussi connues en tan
Soyez attentif aux commentaires dans le code suivant :

```js
var a = 4; // a est une variable globale, elle est accessible dans les fonctions ci-dessous
const a = 4; // a est une variable globale, elle est accessible dans les fonctions ci-dessous

function foo() {
var b = a * 3; // b n'est pas accessible hors de la fonction foo mais l'est
// dans les fonctions déclarées à l'intérieur de foo
function bar(c) {
var b = 2; // une autre variable `b` est créée à l'intérieur du scope de la fonction
// les changements apportés à cette nouvelle variable `b` n'ont pas d'effet sur
// l'ancienne variable `b`
console.log( a, b, c );
}

bar(b * 4);
let b = a * 3; // b n'est pas accessible hors de la fonction foo mais l'est
// dans les fonctions déclarées à l'intérieur de foo

function bar(c) {
let b = 2; // une autre variable `b` est créée à l'intérieur du scope de la fonction
// les changements apportés à cette nouvelle variable `b` n'ont pas d'effet sur
// l'ancienne variable `b`
console.log( a, b, c );
}

bar(b * 4);
}

foo(); // 4, 2, 48
Expand All @@ -28,40 +29,40 @@ foo(); // 4, 2, 48
IIFE, Immediately Invoked Function Expression, est un schéma commun pour créer des scopes locaux :

```js
(function(){ // l'expression `function` est entourée par des parenthèses
(function(){ // l'expression `function` est entourée par des parenthèses
// les variables définies ici
// ne sont pas accessibles en dehors
})(); // la fonction est appelée immédiatement
})(); // la fonction est appelée immédiatement
```
## Le défi :

Créez un fichier nommé `scope.js`.

Dans ce fichier, copiez le code suivant :
```js
var a = 1, b = 2, c = 3;
let a = 1, b = 2, c = 3;

(function firstFunction(){
var b = 5, c = 6;
let b = 5, c = 6;

(function secondFunction(){
var b = 8;
(function secondFunction(){
let b = 8;

(function thirdFunction(){
var a = 7, c = 9;
(function thirdFunction(){
let a = 7, c = 9;

(function fourthFunction(){
var a = 1, c = 8;
(function fourthFunction(){
let a = 1, c = 8;

})();
})();
})();
})();
})();
})();
})();
```

Utilisez vos connaissances des `scopes` de variables et placez le code suivant à l'intérieur d'une fonction de `scope.js` afin d'obtenir la sortie `a: 1, b: 8, c: 6`
```js
console.log("a: "+a+", b: "+b+", c: "+c);
console.log(`a: ${a}, b: ${b}, c: ${c}`);
```

Vérifiez si votre programme est correct en exécutant la commande :
Expand Down
48 changes: 24 additions & 24 deletions problems/scope/problem_it.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,60 @@ Le funzioni definite all'interno di altre funzioni, note come funzioni annidate,
Presta attenzione ai commenti nel codice seguente:

```js
var a = 4; // a è una variabile globale, può essere acceduta dalle funzioni seguenti
const a = 4; // a è una variabile globale, può essere acceduta dalle funzioni seguenti

function foo() {
var b = a * 3; // b non può essere acceduta fuori dalla funzione foo, ma può essere acceduta dalle funzioni
// definite all'interno di foo
function bar(c) {
var b = 2; // un'altra variabile `b` è creata all'interno dell'ambito della funzione bar
// i cambiamenti a questa nuova variabile `b` non hanno effetto sulla variabile `b` precedente
console.log( a, b, c );
}

bar(b * 4);
let b = a * 3; // b non può essere acceduta fuori dalla funzione foo, ma può essere acceduta dalle funzioni
// definite all'interno di foo
function bar(c) {
let b = 2; // un'altra variabile `b` è creata all'interno dell'ambito della funzione bar
// i cambiamenti a questa nuova variabile `b` non hanno effetto sulla variabile `b` precedente
console.log( a, b, c );
}

bar(b * 4);
}

foo(); // 4, 2, 48
```
IIFE, _Immediately Invoked Function Expression_ ovvero espressione di funzione invocata immediatamente, è un pattern comune per creare ambiti locali
esempio:
```js
(function(){ // l'espressione di funzione è circondata da parentesi
(function(){ // l'espressione di funzione è circondata da parentesi
// le variabili definite qui
// non possono essere accedute dall'esterno
})(); // la funzione è invocata immediatamente
})(); // la funzione è invocata immediatamente
```
## La sfida:

Crea un file dal nome `scope.js`.

In questo file, copia il codice seguente:
```js
var a = 1, b = 2, c = 3;
let a = 1, b = 2, c = 3;

(function firstFunction(){
var b = 5, c = 6;
let b = 5, c = 6;

(function secondFunction(){
var b = 8;
(function secondFunction(){
let b = 8;

(function thirdFunction(){
var a = 7, c = 9;
(function thirdFunction(){
let a = 7, c = 9;

(function fourthFunction(){
var a = 1, c = 8;
(function fourthFunction(){
let a = 1, c = 8;

})();
})();
})();
})();
})();
})();
})();
```

Usa la tua comprensione dell'`ambito` delle variabili e posiziona il codice seguente dentro una delle funzioni in `scope.js`
in maniera tale che il risultato sia `a: 1, b: 8,c: 6`
```js
console.log("a: "+a+", b: "+b+", c: "+c);
console.log(`a: ${a}, b: ${b}, c: ${c}`);
```

Verifica che il tuo programma sia corretto eseguendo questo comando:
Expand Down
Loading