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

segundo capítulo #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
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
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
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
}

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

console.log("Strings juntas:", juntarStrings("Hello, ", "World!"));
console.log("Strings juntas:", juntarStrings2("Hello, ", "World!"));
9 changes: 8 additions & 1 deletion Chapter2/Exercise10/Palindromo.js
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) {
Copy link
Member

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, but 5 === 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 ;)

Copy link
Author

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

return true
} else {
return false
}
}

let palavra = "arara";
console.log("É palíndromo:", Palindromo(palavra)); //output: true
4 changes: 3 additions & 1 deletion Chapter2/Exercise6/Inverterstring.js
Copy link
Member

Choose a reason for hiding this comment

The 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
11 changes: 10 additions & 1 deletion Chapter2/Exercise7/removerVogais.js
Copy link
Member

Choose a reason for hiding this comment

The 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 .filter method on that array. The .filter method removes or keeps items from an array depending on if the condition you return is true or false (keeps the value if it is true removes it if it is false).

The condition I used inside the filter is the same one you did, the only difference is that I applied the .toLowerCase() method so I don't need to specify the vowels in upper case too.

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
10 changes: 9 additions & 1 deletion Chapter2/Exercise8/eliminarEspacos.js
Copy link
Member

Choose a reason for hiding this comment

The 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
4 changes: 3 additions & 1 deletion Chapter2/Exercise9/contarPalavra.js
Copy link
Member

Choose a reason for hiding this comment

The 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