Skip to content

primeiro capítulo #1

Open
gxnca wants to merge 1 commit intomainfrom
gf/chapter1
Open

primeiro capítulo #1
gxnca wants to merge 1 commit intomainfrom
gf/chapter1

Conversation

@gxnca
Copy link
Copy Markdown
Member

@gxnca gxnca commented Nov 19, 2024

No description provided.

@gxnca gxnca requested review from diogogmatos and nunom27 November 19, 2024 23:53
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!


function juntarStrings(s1, s2) {}
function juntarStrings(s1, s2) {
return s1.concat(s2)
Copy link
Copy Markdown
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