-
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
quarto capítulo #4
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 |
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 |
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 |
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 |
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 |
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 |
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 ] |
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 |
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 |
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 |
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! |
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] |
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! |
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. Pretty clean. |
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. |
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 |
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!
Another cool possibility: