-
Notifications
You must be signed in to change notification settings - Fork 0
primeiro capítulo #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
| 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 |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!")); | ||
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:The
...beforenumerosis called the spread operator, it takes the array and unpacks its elements into individual arguments. This is necessary becauseMath.max()does not receive arrays, but individual numbers like so:Math.max(1,2).