diff --git a/Chapter1/Exercise1/maiorNumero.js b/Chapter1/Exercise1/maiorNumero.js index 017dd14..67afd6a 100644 --- a/Chapter1/Exercise1/maiorNumero.js +++ b/Chapter1/Exercise1/maiorNumero.js @@ -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 diff --git a/Chapter1/Exercise2/Media.js b/Chapter1/Exercise2/Media.js index 426df12..046d767 100644 --- a/Chapter1/Exercise2/Media.js +++ b/Chapter1/Exercise2/Media.js @@ -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 diff --git a/Chapter1/Exercise3/segundomaior.js b/Chapter1/Exercise3/segundomaior.js index a87f889..7394e69 100644 --- a/Chapter1/Exercise3/segundomaior.js +++ b/Chapter1/Exercise3/segundomaior.js @@ -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 diff --git a/Chapter1/Exercise4/contarDigitos.js b/Chapter1/Exercise4/contarDigitos.js index 603239f..5ca8b03 100644 --- a/Chapter1/Exercise4/contarDigitos.js +++ b/Chapter1/Exercise4/contarDigitos.js @@ -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 diff --git a/Chapter1/Exercise5/juntarStrings.js b/Chapter1/Exercise5/juntarStrings.js index d13d0a6..f60e565 100644 --- a/Chapter1/Exercise5/juntarStrings.js +++ b/Chapter1/Exercise5/juntarStrings.js @@ -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!")); diff --git a/Chapter2/Exercise10/Palindromo.js b/Chapter2/Exercise10/Palindromo.js index fe2b7f7..9ed4d11 100644 --- a/Chapter2/Exercise10/Palindromo.js +++ b/Chapter2/Exercise10/Palindromo.js @@ -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 diff --git a/Chapter2/Exercise6/Inverterstring.js b/Chapter2/Exercise6/Inverterstring.js index e55abc0..66a11e3 100644 --- a/Chapter2/Exercise6/Inverterstring.js +++ b/Chapter2/Exercise6/Inverterstring.js @@ -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 diff --git a/Chapter2/Exercise7/removerVogais.js b/Chapter2/Exercise7/removerVogais.js index 4a19a0b..ba81beb 100644 --- a/Chapter2/Exercise7/removerVogais.js +++ b/Chapter2/Exercise7/removerVogais.js @@ -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 diff --git a/Chapter2/Exercise8/eliminarEspacos.js b/Chapter2/Exercise8/eliminarEspacos.js index e132c43..3f660b1 100644 --- a/Chapter2/Exercise8/eliminarEspacos.js +++ b/Chapter2/Exercise8/eliminarEspacos.js @@ -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 diff --git a/Chapter2/Exercise9/contarPalavra.js b/Chapter2/Exercise9/contarPalavra.js index eb2c3b3..5fd0170 100644 --- a/Chapter2/Exercise9/contarPalavra.js +++ b/Chapter2/Exercise9/contarPalavra.js @@ -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