-
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
segundo capítulo #2
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 |
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 |
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 | ||
} | ||
|
||
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) | ||
} | ||
|
||
console.log("Strings juntas:", juntarStrings("Hello, ", "World!")); | ||
console.log("Strings juntas:", juntarStrings2("Hello, ", "World!")); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
//Escreve uma função que verifique se uma palavra lida de trás para frente é igual à leitura normal. | ||
|
||
function Palindromo(s) {} | ||
function Palindromo(s) { | ||
const reversed = s.split("").reverse().join("") | ||
if (reversed == s) { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
let palavra = "arara"; | ||
console.log("É palíndromo:", Palindromo(palavra)); //output: true |
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. Perfect 👌🏻 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
//Escreve uma função que inverta a ordem dos caracteres de uma string. | ||
|
||
function inverter(s) {} | ||
function inverter(s) { | ||
return s.split("").reverse().join("") | ||
} | ||
|
||
let string = "exemplo"; | ||
console.log("String invertida:", inverter(string)); //output: olpmexe |
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 works! A crazy way to do it in one line (and this is why I love JS xD) would be this: console.log("example".split("").filter(char => !"aeiou".includes(char.toLowerCase())).join("")) We're transforming "example" into an array, like you did in other exercises, and then using the The condition I used inside the filter is the same one you did, the only difference is that I applied the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
//Escreve uma função que remova todas as vogais de uma string | ||
|
||
function removerVogais(s) {} | ||
function removerVogais(s) { | ||
const vowels = "aeiouAEIOU" | ||
let str = "" | ||
for (item of s) { | ||
if (!vowels.includes(item)) { | ||
str += item | ||
} | ||
} | ||
return str | ||
} | ||
|
||
let string = "exemplo"; | ||
console.log("String sem vogais:", removerVogais(string)); //output: xmpl |
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. Nice job! Another crazy one-line alternative solution, using the same concept as my comment in the previous exercise: let stringComEspacos = "a a bb a";
console.log(stringComEspacos.split("").filter(char => !(char === " ")).join("")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
// Escreve uma função que elimine espaços extras em uma string, deixando apenas um espaço entre as palavras | ||
|
||
function eliminarEspacos(s) {} | ||
function eliminarEspacos(s) { | ||
str = "" | ||
for (item of s.split("")) { | ||
if (item!=" ") { | ||
str += item | ||
} | ||
} | ||
return str | ||
} | ||
|
||
let stringComEspacos = "a a bb a"; | ||
console.log("String sem espaços extras:", eliminarEspacos(stringComEspacos)); //output: aabba |
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. Perfect! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
//Escreve uma função que conte quantas palavras existem em uma string separadas por espaços. | ||
|
||
function contarPalavrasSimples(s) {} | ||
function contarPalavrasSimples(s) { | ||
return s.split(" ").length | ||
} | ||
|
||
let frase = "exemplo de uma frase simples"; | ||
console.log("Número de palavras:", contarPalavrasSimples(frase)); //output: 5 |
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! Just a little warning, in JavaScript we have two operators for comparison:
==
and===
. While the first tries to convert both arguments to the same type (for example:'5' == 5
would be true), the last strictly compares both items, meaning they have to have the same value (for example:'5' === 5
would be false, but5 === 5
would be true). Usually, we should always use===
to avoid problems, unless we explicitly want to compare values not strictly.In this case it would work either way, but since we know we are just comparing strings, there's no need to use
==
.On a final note, we could make this
if ... else ...
into just one line, like so:return reversed === s
;)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.
Uhhh, great advice.
I'll make sure I'll get it right next time :)