-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
base: master
Are you sure you want to change the base?
Conversation
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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; |
There was a problem hiding this comment.
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???
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; |
There was a problem hiding this comment.
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
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. |
Solved lab + bonus