Skip to content

Commit

Permalink
Merge pull request #137 from josemoracard/jose4-07.2-letter-counter
Browse files Browse the repository at this point in the history
exercises 07.2-letter-counter to 14-divide-and-conquer
  • Loading branch information
alesanchezr authored Sep 6, 2023
2 parents ea6cf33 + 9306d71 commit 9cdc105
Show file tree
Hide file tree
Showing 53 changed files with 285 additions and 289 deletions.
6 changes: 3 additions & 3 deletions exercises/07.2-Letter-Counter/README.es.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `07.2` Contador de letras
# `07.2` Letter Counter

Nuestro cliente necesita un programa que cuente las repeticiones de las letras en un string dado. Sé que es extraño, pero es muy testarudo ¡Lo necesitamos lo antes posible!

## :pencil: Instrucciones:
## 📝 Instrucciones:

1. Crea un objeto donde las letras son las propiedades y los valores son el número de veces que esa letra se repite en toda la cadena.

Expand All @@ -14,7 +14,7 @@ const word = "Hello World";
// Debería imprimir en la consola { h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1 }
```

## :bulb: Pista:
## 💡 Pistas:

+ Recorre todo el string (usa un bucle).

Expand Down
14 changes: 6 additions & 8 deletions exercises/07.2-Letter-Counter/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---

tutorial: https://www.youtube.com/watch?v=oLTidCuisew

---

# `07.2` Letter Counter

Our customer needs a program that counts the number of occurences of each letter in a given string. I know that's weird, but they are very adamant. We need this asap!
Our customer needs a program that counts the number of occurrences of each letter in a given string. I know that's weird, but they are very adamant. We need this asap!

## :pencil: Instructions:
## 📝 Instructions:

1. Create an object where the letters are the properties and the values are the number of times that letter is repeated throughout the string.

Expand All @@ -20,16 +18,16 @@ const word = "Hello World";
// The console should print { h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1 }
```

## :bulb: Hint
## 💡 Hints:

+ Loop the entire string.

+ On every iteration check if the object `counts` has the letter initialized as a property.
+ On every iteration, check if the object `counts` has the letter initialized as a property.

+ If the letter has not been initialized, then do it and set its value equal to 1 (first time found).

+ If it was already initialized just increment the property value by one.
+ If it was already initialized, just increment the property value by one.

+ Remember to ignore white spaces in the string.

+ You should lower case all letters to have an accurate count of all letters regardless of casing of the letter.
+ To accurately count all letters, regardless of their casing, you should convert all letters to lowercase.
4 changes: 2 additions & 2 deletions exercises/07.2-Letter-Counter/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let par = "Lorem ipsum dolor sit amet consectetur adipiscing elit Curabitur eget bibendum turpis Curabitur scelerisque eros ultricies venenatis mi at tempor nisl Integer tincidunt accumsan cursus"
let counts = {};

// your code here
// Your code here

console.log(counts);
console.log(counts);
14 changes: 5 additions & 9 deletions exercises/07.2-Letter-Counter/solution.hide.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
let par = "Lorem ipsum dolor sit amet consectetur adipiscing elit Curabitur eget bibendum turpis Curabitur scelerisque eros ultricies venenatis mi at tempor nisl Integer tincidunt accumsan cursus"
let counts = {};

// your code here
for(let i in par){
// Your code here
for(let i in par) {
const letter = par[i].toLowerCase();
console.log(letter);
if(letter == " ") continue;
else if(counts[letter] == undefined){
console.log("Found "+letter+" for the first time")
else if(counts[letter] == undefined) {
counts[letter] = 1;
}
else{
console.log("Found "+letter+" more than once")
else {
counts[letter] = counts[letter] + 1;

}
}

console.log(counts);
console.log(counts);
4 changes: 2 additions & 2 deletions exercises/07.2-Letter-Counter/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it('You have to use the console.log function once, at the end of the exercise',
expect(console.log.mock.calls.length).toBe(1);
});

it('Use a for loop', function () {
it('Use a "for" loop', function () {
const app_content = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8');
expect(app_content).toMatch(/for(\s*)\(/);
});
Expand All @@ -37,4 +37,4 @@ it('Create the object with the letter counts like { a: 1, b: 4, ... }', function
}

expect(counts).toEqual(_counts);
});
});
14 changes: 7 additions & 7 deletions exercises/07.3-Flip-Array/README.es.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# `07.3` Invierte el Array
# `07.3` Flip Array

## :pencil: Instrucciones:
## 📝 Instrucciones:

1. Usando un bucle `for`, invierte el arreglo o array `arr` e imprime el nuevo arreglo o array en la consola.
1. Usando un bucle `for`, invierte el array `arr` e imprime el nuevo array en la consola.

Por ejemplo:

```js
array inicial: [45, 67, 87, 23, 5, 32, 60]; array array final : [60, 32, 5 , 23, 87, 67, 45];
Initial array: [45, 67, 87, 23, 5, 32, 60];
Final array: [60, 32, 5, 23, 87, 67, 45];
```

## :bulb: Pista
## 💡 Pistas:

+ Debes recorrer todo el arreglo [desde el final hasta el principio](https://stackoverflow.com/questions/1340589/are-loops-really-faster-in-reverse).

+ En cada bucle, inserta todos los elementos (a
medida que avanza) en un nuevo array o arreglo, este será tu arreglo invertido.
+ En cada bucle, inserta todos los elementos (a medida que avanza) en un nuevo array, este será tu arreglo invertido. ¿Qué otros métodos puedes usar además de `push()`?
12 changes: 6 additions & 6 deletions exercises/07.3-Flip-Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ tutorial: https://www.youtube.com/watch?v=Snn7OtZY370

# `07.3` Flip Array

## :pencil: Instructions:
## 📝 Instructions:

1. Using a `for` loop, invert the `arr` array and print the new array on the console.

For example:

```js
Initial array: [45, 67, 87, 23, 5, 32, 60];
Final array: [60, 32, 5 , 23, 87, 67, 45];
Initial array: [45, 67, 87, 23, 5, 32, 60];
Final array: [60, 32, 5, 23, 87, 67, 45];
```

## :bulb: Hint
## 💡 Hints:

+ You should loop the entire array [from the end to the beggining](https://stackoverflow.com/questions/1340589/are-loops-really-faster-in-reverse).
+ You should loop the entire array [from the end to the beginning](https://stackoverflow.com/questions/1340589/are-loops-really-faster-in-reverse).

+ On each loop push all the items (as you go) into a new array, this will be your flipped array. What other methods can you use besides `push()`?
+ On each loop, push all the items (as you go) into a new array, this will be your flipped array. What other methods can you use besides `push()`?
2 changes: 1 addition & 1 deletion exercises/07.3-Flip-Array/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let arr = [45,67,87,23,5,32,60];

//Your code here.
// Your code here
7 changes: 4 additions & 3 deletions exercises/07.3-Flip-Array/solution.hide.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
let arr = [45,67,87,23,5,32,60];

//Your code here.
// Your code here
let flippedArray = []
for(let i = arr.length - 1; i>= 0;i--){
for(let i = arr.length - 1; i >= 0; i--) {
let item = arr[i];
flippedArray.push(item);
}
console.log(flippedArray)

console.log(flippedArray)
13 changes: 4 additions & 9 deletions exercises/07.3-Flip-Array/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@ global.console.log = console.log = jest.fn((text) => _buffer += text + "\n");
let reverse = Array.prototype.reverse;
Array.prototype.reverse = jest.fn(function(){ return this; });

it('Use the console.log function once to print the new array with the types on the console', function () {
it('Use the console.log function once, at the end of the exercise', function () {
const app = require('./app.js');
expect(console.log.mock.calls.length).toBe(1);
});

it("Use the typeof function inside the loop", function () {
const app_content = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8');
expect(app_content).not.toMatch(/\.typeof/);
});

it('There needs to be a variable called arr with the original array', function () {
it('There needs to be a variable called "arr" with the original array', function () {
const _app = rewire('./app.js');
const variable = _app.__get__('arr');
expect(variable).toBeTruthy();
});

it('Loop the array in a reverse order and console.log all of its item', function () {
it('Loop the array in a reverse order and console.log all of its items', function () {
const _app = rewire('./app.js');
const variable = _app.__get__('arr');
let inverted = [];
for(let i = variable.length-1; i>=0;i--){
inverted.push(variable[i]);
}
expect(_buffer).toMatch(inverted.map(n => n).join(","));
});
});
16 changes: 8 additions & 8 deletions exercises/08.1-Mixed-array/README.es.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# `08.01` Array mixto
# `08.1` Mixed Array

## :pencil: Instrucciones:
## 📝 Instrucciones:

1. Escribe una función que imprima un arreglo en la consola que contenga los tipos de valores (data-types) que contiene el array `mix` en cada posición.
1. Usando un bucle, imprime un nuevo array en la consola que contenga los tipos de valores (data-types) que contiene el array `mix` en cada posición.

### Resultado esperado:
## 💻 Resultado esperado:

```js
[
Expand All @@ -15,14 +15,14 @@
]
```

## :bulb: Pista
## 💡 Pista

+ Crea un nuevo arreglo vacío.

+ Recorre el arreglo original mediante un bucle.

+ En cada bucle, obten el tipo de elemento utilizando la función `typeof`.
+ En cada bucle, obtén el tipo de elemento utilizando la función `typeof`.

+ Como la función `typeof` devuelve un string, puedes insertar(push) ese string en el nuevo arreglo(array).
+ Como la función `typeof` devuelve un string, puedes insertar(push) ese string en el nuevo arreglo.

+ Cuando finalice el bucle o loop, debes haber encontrado todos los tipos de elementos del arreglo o array original y haberlos insertados(push) en el nuevo arreglo.
+ Cuando finalice el bucle, debes haber encontrado todos los tipos de elementos del arreglo original y haberlos insertado(push) en el nuevo arreglo.
16 changes: 8 additions & 8 deletions exercises/08.1-Mixed-array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
tutorial: https://www.youtube.com/watch?v=3o02odJhieo
---

# `08.01` Mixed Array
# `08.1` Mixed Array

## :pencil: Instructions:
## 📝 Instructions:

1. Write a function that prints in the console a new array that contains all the types of data that the array `mix` contains in each position.
1. Using a loop, print in the console a new array that contains all the types of data that the array `mix` contains in each position.

### Expected Result:
## 💻 Expected Result:

```js
[
Expand All @@ -19,14 +19,14 @@ tutorial: https://www.youtube.com/watch?v=3o02odJhieo
]
```

## :bulb: Hints:
## 💡 Hints:

+ Create a new empty array.

+ Loop the original array.

+ On every loop get the type of the item using the `typeof` function.
+ On every loop, get the type of the item using the `typeof` function.

+ Since the `typeof` function return a string you can push that string to the new array.
+ Since the `typeof` function returns a string you can push that string to the new array.

+ when the loop finished, you should have all the types found on the original array pushed to the new array.
+ When the loop is finished, you should have all the types found on the original array pushed to the new array.
2 changes: 1 addition & 1 deletion exercises/08.1-Mixed-array/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let mix = [42, true, "towel", [2,1], 'hello', 34.4, {"name": "juan"}];

//your code here
// Your code here
6 changes: 3 additions & 3 deletions exercises/08.1-Mixed-array/solution.hide.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let mix = [42, true, "towel", [2,1], 'hello', 34.4, {"name": "juan"}];

//your code here
// Your code here
let newArray = [];
for (let i = 0; i < mix.length; i++) {
const item = mix[i];
newArray.push(typeof item)

}
console.log(newArray)

console.log(newArray)
9 changes: 2 additions & 7 deletions exercises/08.1-Mixed-array/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ it("Create a loop", function () {
expect(app_content).toMatch(/for\s*/);
});

it("Don't use the reverse function", function () {
const app_content = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8');
expect(app_content).not.toMatch(/\.reverse(\s*)\(/);
});

it('Use the console.log function once to print the newArray on the console', function () {
const app = require('./app.js');
expect(console.log.mock.calls.length).toBe(1);
});

it('There needs to be a variable called mix with the original array', function () {
it('There needs to be a variable called "mix" with the original array', function () {
const _app = rewire('./app.js');
const variable = _app.__get__('mix');
expect(variable).toBeTruthy();
Expand All @@ -45,4 +40,4 @@ it('Loop the array and console.log all of its item types', function () {
let _test = myFunc()
expect(_buffer).toMatch(_test.map(n => n).join(","));
// expect(_buffer).toMatch(inverted.map(n => n).join(","));
});
});
16 changes: 9 additions & 7 deletions exercises/08.2-Count-On/README.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

Como viste en el último ejercicio, tu arreglo o array puede tener una mezcla de tipos de datos.

## :pencil: Instrucciones
## 📝 Instrucciones:

1. Agrega todos los elementos con tipo de dato objeto dentro del array `hello`.

Aquí puedes ver cómo imprimir TODOS los elementos.
Aquí puedes ver cómo imprimir los tipos de elementos:

```js
let myArray = [42, true, "towel", [2,1], 'hello', 34.4, {"name": "juan"}];

for(let index = 0; index < myArray.length; index++){
let item = myArray[index];
for(let i = 0; i < myArray.length; i++) {
let item = myArray[i];
console.log(typeof(item))
}
```

## :bulb: Pista
> Nota: Te darás cuenta que cuando verificas el tipo de dato de un array, te devolverá que es un `'object'`. Puedes investigar el por qué en Google o proceder con el ejercicio como de costumbre.
## 💡 Pistas:

+ Recorre el array dado con un loop.

+ Agrega una condición dentro del bucle(loop) que verifique que el elemento sea un objeto.
+ Agrega una condición dentro del bucle que verifique que el elemento sea un objeto.

+ Si el elemento es un objeto, se agrega al arreglo `hello`.

+ Usa `console.log()`para imprimir el array `hello` en la consola.
+ Usa `console.log()` para imprimir el array `hello` en la consola.
Loading

0 comments on commit 9cdc105

Please sign in to comment.