Skip to content

Néboa - lab-javascript-functions-and-arrays #3839

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

Conversation

0000nebu
Copy link

@0000nebu 0000nebu commented Jun 5, 2023

Solved lab + bonus

Comment on lines +37 to +46
let sum = 0
if (numbers.length === 0){
return 0;
} else {
for (let i = 0; i<numbers.length; i++){
let number = numbers[i]
sum += number;
}
}
return sum

Choose a reason for hiding this comment

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

Suggested change
let sum = 0
if (numbers.length === 0){
return 0;
} else {
for (let i = 0; i<numbers.length; i++){
let number = numbers[i]
sum += number;
}
}
return sum
if (numbers.length === 0){
return 0;
}
return sum(numbers);

Ya que te has hecho el metodo sum! pues vamos a usarlo! :D

Comment on lines +54 to +75
let countChar = 0;
let countNum = 0;
let countBol = 0;

if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
let sumNumChar = countChar + countNum+ countBol;
return sumNumChar;

Choose a reason for hiding this comment

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

Suggested change
let countChar = 0;
let countNum = 0;
let countBol = 0;
if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
let sumNumChar = countChar + countNum+ countBol;
return sumNumChar;
let count = 0;
if (mixedArr.length === 0) {
return 0;
} else {
for (let i = 0; i < mixedArr.length; i++) {
let element = mixedArr[i];
if (typeof element === 'number' || typeof element === 'boolean') {
count += element;
} else if (typeof element === 'string') {
count += element.length;
} else {
throw new Error("Unsupported data type sir or ma'am");
}
}
}
return count;

Bien!, entiendo perfectamente lo de las 3 variables, pero fijate la propia definicion del metodo te dice que es una suma, es decir que todo tendra que ser un numero!, por ello, si te creas una unica variable que sea de contador lo tendrias, ahora bien, veras que he puesto los numeros y los booleanos juntos, esto es porque si tu intentas sumar un booleano hace lo siguiente, true = 1 y false = 0, por ello si intentas hacer const x = 1 + true, x valdria 2! :D

Comment on lines +118 to +137
let countChar = 0;
let countNum = 0;
let countBol = 0;

if (wordsArr.length === 0) {
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let element = wordsArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
}
}
}
let sumNumChar = countChar + countNum + countBol;
return sumNumChar / wordsArr.length;

Choose a reason for hiding this comment

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

Suggested change
let countChar = 0;
let countNum = 0;
let countBol = 0;
if (wordsArr.length === 0) {
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let element = wordsArr[i];
if (typeof element === 'number') {
countNum += element;
} else if (typeof element === 'string') {
countChar += element.length;
} else if (typeof element === 'boolean'){
countBol += element;
}
}
}
let sumNumChar = countChar + countNum + countBol;
return sumNumChar / wordsArr.length;
return sum(wordsArr)/ wordsArr.length;

Fijate!, has repetido todo el codigo que estaba en sum, pues entonces para ver la potencia de las funciones ejecutamos sum, repito, ejecutamos pasandole como argumento el array y listo!, tendriamos lo mismo!!! :D

Comment on lines +85 to +95
let sum = 0
if (numbersAvg.length === 0){
return null;
} else {
for (let i = 0; i < numbersAvg.length; i++) {
let number = numbersAvg[i];
sum += number;
}
}

return sum / numbersAvg.length;

Choose a reason for hiding this comment

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

Viendo las correcciones que te he puesto antes con la clase sum, este fragmento se podria hacer de otra forma no? mmmmm como lo harias???

Comment on lines +103 to +112
let count = 0;
if (wordsArr.length === 0){
return null;
} else {
for (let i = 0; i < wordsArr.length; i++) {
let char = wordsArr[i];
count += char.length;
}
}
return count / wordsArr.length;

Choose a reason for hiding this comment

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

Con este fragmento de codigo me pasa lo mismo!, esto se parece al metodo sum no? xD

@stale
Copy link

stale bot commented Jul 15, 2023

This pull request has been automatically marked as stale because it didn't have any recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants