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

quarto capítulo #4

Open
wants to merge 4 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) {
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
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
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
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
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
17 changes: 15 additions & 2 deletions Chapter3/Exercise11/minimo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
//Escreve uma função que encontre o menor número em um array de inteiros.

function minimoSimples(arr) {}
function minimoSimples(arr) {
let min = 1000000
for (item of arr) {
if (min>item) {
min = item
}
} return min
}

// ou

function minimoSimples2(arr) {
return Math.min(...arr)
}

let numeros = [10, 5, 8, 1, 7];
console.log("Menor número:", minimoSimples(numeros)); //output: 1
console.log("Menor número:", minimoSimples2(numeros)); //output: 1
9 changes: 8 additions & 1 deletion Chapter3/Exercise12/removerDuplicados.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Escreve uma função que remova todos os elementos duplicados de um array.

function removeDuplicados(arr) {}
function removeDuplicados(arr) {
let arrF = []
for (item of arr) {
if (!arrF.includes(item)) {
arrF.push(item)
}
} return arrF
}

let arrDuplicados = [1, 2, 3, 2, 1, 4, 5];
console.log("Array sem duplicatas:", removeDuplicados(arrDuplicados)); //output: [ 1, 2, 3, 4, 5 ]
8 changes: 7 additions & 1 deletion Chapter3/Exercise13/contarOcorrencias.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//Escreve uma função que conte quantas vezes um número aparece em um array.

function contarOcorrencias(arr, num) {}
function contarOcorrencias(arr, num) {
let i = 0
for (item of arr) {
if (num == item)
i++
}return i
}

let numerosArr = [1, 2, 3, 2, 1, 4, 2];
console.log("Ocorrências de 2:", contarOcorrencias(numerosArr, 2)); //output: 3
9 changes: 8 additions & 1 deletion Chapter3/Exercise14/somarPares.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Escreve uma função que some todos os números pares de um array

function somarPares(arr) {}
function somarPares(arr) {
let soma = 0
for (item of arr) {
if (item % 2 == 0) {
soma += item
}
} return soma
}

let numerosPares = [1, 2, 3, 4, 5, 6];
console.log("Soma dos pares:", somarPares(numerosPares)); //output: 12
9 changes: 8 additions & 1 deletion Chapter3/Exercise15/contarDiferentes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Escreve uma função que conte quantos caracteres únicos existem em uma string

function contarDiferentes(s) {}
function contarDiferentes(s) {
let str = []
for (item of s) {
if (!str.includes(item)) {
str.push(item)
}
} return str.length
}

let stringDiferente = "aabbccdde";
console.log("Caracteres diferentes:", contarDiferentes(stringDiferente)); //output: 5
9 changes: 8 additions & 1 deletion Chapter4/Exercise16/juntaArrays.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Another cool possibility:

return [...arr1, ...arr2]

Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Escreve uma função que mescle dois arrays e remova os elementos duplicados do array resultante

function juntaArrays(arr1, arr2) {}
function juntaArrays(arr1, arr2) {
let narr = []
for (item of arr1.concat(arr2)) {
if (!narr.includes(item)) {
narr.push(item)
}
}return narr
}

let arr1 = [1, 2, 3];
let arr2 = [3, 4, 5];
Expand Down
10 changes: 9 additions & 1 deletion Chapter4/Exercise17/twoSum.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!

Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Dado um array de inteiros nums e um inteiro target, retorne os índices de dois números no array
// que somam o valor target.

function twoSum(nums, target) {}
function twoSum(nums, target) {
for (item of nums) {
for (itemI of (nums.filter(x => x !== item))) {
if (item + itemI == target) {
return [nums.indexOf(item), nums.indexOf(itemI)]
}
}
}
}

console.log(twoSum([2, 7, 11, 15], 9)); //output: [0, 1]
16 changes: 15 additions & 1 deletion Chapter4/Exercise18/fizzBuzz.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Escreve um programa que imprima números de 1 a n. Para múltiplos de 3, imprima "Fizz" em vez do número,
// e para múltiplos de 5, imprima "Buzz". Para números múltiplos de 3 e 5, imprima "FizzBuzz".

function fizzBuzz(n) {}
function fizzBuzz(n) {
for (let i=1; i<=n; i++) {
if (i % 3 == 0) {
if (i % 5 == 0) {
console.log("FizzBuzz")
} else {
console.log("Fizz")
}
} else if (i % 5 == 0) {
console.log("Buzz")
} else {
console.log(i)
}
}
}

fizzBuzz(15);
//output:
Expand Down
21 changes: 6 additions & 15 deletions Chapter4/Exercise19/eAnagrama.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty clean.

Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
// Dadas duas strings, verifica se uma é o anagrama da outra (ou seja, se contêm os mesmos caracteres, com a mesma frequência, mas em qualquer ordem).

function eAnagrama(str1, str2) {
if (str1.length !== str2.length) return false;
if (!str1.length == str2.length) return false

let contador1 = {};
let contador2 = {};

for (let i = 0; i < str1.length; i++) {
let char1 = str1[i];
let char2 = str2[i];

contador1[char1] = (contador1[char1] || 0) + 1;
contador2[char2] = (contador2[char2] || 0) + 1;
for (item of str1) {
if (!str2.includes(item)) return false
}

for (let key in contador1) {
if (contador1[key] !== contador2[key]) {
return false;
}
for (item of str2) {
if (!str1.includes(item)) return false
}

return true;
return true
}

console.log(eAnagrama("listen", "silent")); //output: true
Expand Down
7 changes: 6 additions & 1 deletion Chapter4/Exercise20/fatorial.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.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Escreve uma função que calcule o fatorial de um número n. O fatorial de n é o produto de todos os números inteiros de 1 a n.

function fatorial(n) {}
function fatorial(n) {
let result = 1 //fatorial de 0 é 1
for (let i = 1; i<=n; i++) {
result *= i
} return result
}

console.log(fatorial(5)); //output: 120