Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Chapter1/Exercise1/maiorNumero.js
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).

Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
//Escreve uma função que receba uma lista de números inteiros e retorna o maior número

function maiorNumero(lista) {}

function maiorNumero(lista) {
let max = 0
for (let i = 0; i < lista.length; i++)
if (max < lista[i]) {
max = lista[i]
}
return max
}

//ouu

function maiorNumero2(lista) {
let max = 0
for (item of lista)
if (max < item) {
max = item
}
return max
}





let numeros = [3, 5, 7, 2, 8];
console.log("Maior número:", maiorNumero(numeros)); //output: 8
9 changes: 7 additions & 2 deletions Chapter1/Exercise2/Media.js
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.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Escreve uma função que calcule a média de uma sequência de números

function media(numero) {}
function media(numero) {
let soma = 0
for (item of numero)
soma += item
return soma/numero.length
}

let numeros = [3, 5, 7, 2, 8];
console.log("Média:", media(numeros)); //output: 6
console.log("Média:", media(numeros)); //output: 5
5 changes: 4 additions & 1 deletion Chapter1/Exercise3/segundomaior.js
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 :)

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Escreve uma função que encontre o segundo maior número em um array de inteiros

function segundoMaior(numeros) {}
function segundoMaior(numero) {
let numerosOrd = numero.sort()
return numerosOrd[(numero.length)-2]
}

let numeros = [3, 5, 7, 2, 8];
console.log("Segundo maior número:", segundoMaior(numeros)); //output: 7
18 changes: 16 additions & 2 deletions Chapter1/Exercise4/contarDigitos.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Escreve uma função que conte quantos dígitos um número inteiro possui

function contarDigitos(n) {}
function contarDigitos2(n) {
let i = 0
while (n > 0) {
n = Math.floor(n / 10)
i++
}
return i
}

// 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!

}

console.log("Numero de digitos:", contarDigitos2(440)); //output: 3

console.log("Numero de digitos:", contarDigitos(440)); //output: 3
6 changes: 4 additions & 2 deletions Chapter1/Exercise5/juntarStrings.js
Original file line number Diff line number Diff line change
@@ -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
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

}

console.log("Strings juntas:", juntarStrings("Hello, ", "World!"));
console.log("Strings juntas:", juntarStrings2("Hello, ", "World!"));