Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

primeiro capítulo #1

wants to merge 1 commit into from

Conversation

gxnca
Copy link

@gxnca gxnca commented Nov 19, 2024

No description provided.

@gxnca gxnca requested review from nunom27 and diogogmatos November 19, 2024 23:53
Copy link
Member

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).

Copy link
Member

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.

Copy link
Member

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
Copy link
Member

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)
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants