Skip to content

[PTREMOTE] Maria Belen Colturi - lab-javascript-functions-and-arrays #3853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
75 changes: 63 additions & 12 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,91 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}

function maxOfTwoNumbers(n1, n2) {
if (n1 > n2) {
return n1;
} else {
return n2;
}
}


// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}
function findLongestWord(words) {
if (words.length === 0) {
return null;
}
let longest = words[0];
for (let i = 0; i < words.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let i = 0; i < words.length; i++) {
for (let i = 1; i < words.length; i++) {

Si te fijas la primera posicion ya la estas cogiendo antes, asi que compararla consigo misma pues como que no tiene mucho sentido jejeje

if (words[i].length > longest.length) {
longest = words[i];
}
}
return longest;
}



// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}

function sumNumbers(numbers) {

let sum = 0;
for (let i = 0; i < numbers.length; i++) {
const currentNumber = numbers[i]
console.log(currentNumber);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(currentNumber);

Esto son pijadas pero vamos siempre que puedas quitar los console.log es bien :D

sum += currentNumber
}
return sum
}


// Iteration #3.1 Bonus:
function sum() {}
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sum(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
currentNumber = numbers[i]
sum += currentNumber
}
return sum
Comment on lines +47 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este metodo y el anterior de sumNumbers se parecen mucho, tirando a que son iguales, creo que en el ejercicio o los test te iba poniendo los diferentes puntos que tenias que tocar, por ejemplo si justo el elemento que estabas iterando era un string, o si era un booleano, objeto, etc

Miratelo si puedes y si tienes alguna duda me dices vale??

}


// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(numbersAvg) {
let average = 0;
if (numbersAvg = [""]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (numbersAvg = [""]) {
if (numbersAvg.length === 0) {

Creo que en los test te dice si el array esta vacio, devuelve null, pues si te fijas tu habias igualado la variable (hubieses necesitado los tres iguales (===) para que fuera una pregunta buena buena jejej) y luego has preguntado porque el array tuviera una posicion y que en esa posicion tuviera un string vacio o "", por ello la mejor forma de saber si un array esta vacio es preguntar por su length y preguntar si ese length es 0, asi te aseguras que ese array AKA "bolsa" esta vacia, si no tiene nada pues esta vacia !!

return null
}
for (let i = 0; i < numbersAvg.length; i++) {
const element = numbersAvg[i];
average += element
return average / numbersAvg.length
}
Comment on lines +65 to +69

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let i = 0; i < numbersAvg.length; i++) {
const element = numbersAvg[i];
average += element
return average / numbersAvg.length
}
for (let i = 0; i < numbersAvg.length; i++) {
const element = numbersAvg[i];
average += element
}
return average / numbersAvg.length

Si lo pones dentro del for, cuando pase una vez, pues ya no sale del bucle! por eso dejamos que el bucle termine y luego retornamos el resultado! :D

}


// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(wordsArray) {
let wordAverage = 0;

if (wordsArray.length === 0) {
return null;
}
for (let i = 0; i < wordsArray.length; i++) {
const word = wordsArray[i].length;
wordAverage += word
return wordAverage / wordsArray.length
}
Comment on lines +82 to +86

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let i = 0; i < wordsArray.length; i++) {
const word = wordsArray[i].length;
wordAverage += word
return wordAverage / wordsArray.length
}
for (let i = 0; i < wordsArray.length; i++) {
const word = wordsArray[i].length;
wordAverage += word
}
return wordAverage / wordsArray.length

Lo mismo que antes :D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gracias por los comentarios Cristian, voy a seguir practicando


}

// Bonus - Iteration #4.1
function avg() {}
Expand All @@ -52,9 +105,7 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}


function uniquifyArray(words) {}

// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
Expand Down
Empty file added tests/functionsAndArrays.js
Empty file.