-
Notifications
You must be signed in to change notification settings - Fork 0
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
primeiro capítulo #1
base: main
Are you sure you want to change the base?
Conversation
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, would have done it the same :)
// ouu | ||
|
||
function contarDigitos(n) { | ||
return n.toString().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.
Really smart, I would have probably done it this way too, nice job!
@@ -1,5 +1,7 @@ | |||
// Escreva uma função que junta duas strings. | |||
|
|||
function juntarStrings(s1, s2) {} | |||
function juntarStrings(s1, s2) { | |||
return s1.concat(s2) |
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.
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.