Conversation
There was a problem hiding this comment.
Nice! Another possible solution is to use Math.max() like this:
let numeros = [3, 5, 7, 2, 8];
console.log("Maior número:", Math.max(...numeros));The ... before numeros is called the spread operator, it takes the array and unpacks its elements into individual arguments. This is necessary because Math.max() does not receive arrays, but individual numbers like so: Math.max(1,2).
There was a problem hiding this comment.
Great, this is the traditional solution! However, JavaScript has some built in methods for iterating through arrays, like .map. For this question, we could use the .reduce method, it iterates through the array like .map(), but can also receive a variable to change on every iteration:
let numeros = [3, 5, 7, 2, 8];
let soma = numeros.reduce((acc, current_number) => acc + current_number, 0);
console.log("Média:", soma / numeros.length);The acc variable starts with the value 0 and on each iteration we add the value of the current number to it. At the end of it all, we get the sum of all numbers, then we just need to divide them by the number of elements in the array.
There was a problem hiding this comment.
Very nice, would have done it the same :)
| // ouu | ||
|
|
||
| function contarDigitos(n) { | ||
| return n.toString().length |
There was a problem hiding this comment.
Really smart, I would have probably done it this way too, nice job!
|
|
||
| function juntarStrings(s1, s2) {} | ||
| function juntarStrings(s1, s2) { | ||
| return s1.concat(s2) |
There was a problem hiding this comment.
That's a great way to do it! If we wanted to go even simpler, we could just do:
return s1 + s2
No description provided.