-
Notifications
You must be signed in to change notification settings - Fork 0
Exercicios Novos #3
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
base: main
Are you sure you want to change the base?
Changes from 2 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function contarOcorrencias(array, n){ | ||
| let contador = 0; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] === n){ | ||
| contador++ | ||
| } | ||
| } | ||
|
|
||
| return contador; | ||
|
|
||
| } | ||
|
|
||
| console.log(contarOcorrencias([1, 2, 3, 2, 4, 2], 2)); | ||
| // Retorna: 3 | ||
|
|
||
| console.log(contarOcorrencias(["oi", "tchau", "oi", "oi"], "oi")); | ||
| // Retorna: 3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| function contarVogais(palavra){ | ||
| let arrayPalavra = palavra.split("") | ||
| let contador = 0; | ||
|
|
||
| for (let i = 0; i < arrayPalavra.length; i++){ | ||
| if (arrayPalavra[i] === "a" || arrayPalavra[i] === "e" || arrayPalavra[i] === "i" || arrayPalavra[i] === "o" || arrayPalavra[i] === "u") { | ||
| contador++; | ||
| } | ||
| } | ||
|
|
||
| return contador; | ||
| } | ||
|
|
||
| console.log(contarVogais("javascript")); | ||
| console.log(contarVogais("programacao")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function copiarArray(array) { | ||
|
|
||
| let copia = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| copia.push(array[i]); | ||
| } | ||
|
|
||
| return copia; | ||
| } | ||
|
|
||
|
|
||
| console.log(copiarArray([1, 2, 3])) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function criarArray(n){ | ||
| let array = []; | ||
|
|
||
| for (let i = 1; i <= n; i++){ | ||
| array.push(i); | ||
| } | ||
|
|
||
| return array; | ||
| } | ||
|
|
||
| console.log(criarArray(5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| let num = [2, 4, 6, 8]; | ||
| let dob = []; | ||
|
|
||
| for (let i = 0; i < num.length; i++){ | ||
| dob.push(num[i] * 2) | ||
| } | ||
|
|
||
| console.log(dob) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function inverterArray(array){ | ||
| let invertido = []; | ||
|
|
||
| for (let i = array.length - 1; i >= 0; i--){ | ||
| invertido.push(array[i]) | ||
| } | ||
|
|
||
| return invertido | ||
| } | ||
|
|
||
|
|
||
| console.log(inverterArray([1, 2, 3, 4, 5])); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| function maiorMenor(array){ | ||
| let maior = array[0]; | ||
| let menor = array[0]; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] > maior){ | ||
| maior = array[i]; | ||
| } | ||
|
|
||
| if (array[i] < menor){ | ||
| menor = array[i]; | ||
| } | ||
| } | ||
|
|
||
| return { maior: maior, menor: menor }; | ||
| } | ||
|
|
||
| console.log(maiorMenor([5, 2, 9, 1, 7])); | ||
| // Retorna: { maior: 9, menor: 1 } ✅ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function maioresQue(array, n){ | ||
| let numM = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] > n){ | ||
| numM.push(array[i]) | ||
| } | ||
| } | ||
|
|
||
| return numM; | ||
| } | ||
|
|
||
| console.log(maioresQue([1, 2, 3, 4, 5], 3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| function mediaPares(array){ | ||
| let soma = 0; | ||
| let contador = 0; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] % 2 === 0){ | ||
| soma += array[i]; | ||
| contador++; | ||
| } | ||
| } | ||
|
|
||
| let media = soma / contador; | ||
|
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. Division by zero returns NaN for odd-only arraysLow Severity When the input array contains no even numbers, |
||
|
|
||
| return media; | ||
| } | ||
|
|
||
| console.log(mediaPares([1, 2, 3, 4, 5, 6])); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function numeroEntre(array, min, max){ | ||
| let minMax = []; | ||
|
|
||
| for(let i = 0; i < array.length; i++){ | ||
| if(array[i] >= min && array[i] <= max){ | ||
| minMax.push(array[i]) | ||
| } | ||
| } | ||
|
|
||
| return minMax | ||
| } | ||
|
|
||
|
|
||
| console.log(numeroEntre([1, 5, 10, 15, 20, 25], 10, 20)); | ||
| // Retorna: [10, 15, 20] | ||
|
|
||
| console.log(numeroEntre([3, 7, 12, 18, 25], 5, 15)); | ||
| // Retorna: [7, 12] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function testeA (array){ | ||
| let b = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] < 0){ | ||
| b.push(array[i]) | ||
| } | ||
| } | ||
|
|
||
| return b | ||
| } | ||
| console.log(testeA([5, -3, 8, -1, 10, -5, 2])) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function separarParImpar(array){ | ||
| let pares = [] | ||
| let impares = [] | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] % 2 === 0){ | ||
| pares.push(array[i]) | ||
| } else { | ||
| impares.push(array[i]) | ||
| } | ||
| } | ||
|
|
||
| return {pares: pares, impares: impares} | ||
| } | ||
|
|
||
|
|
||
| console.log(separarParImpar([1, 2, 3, 4, 5, 6])); | ||
| // Retorna: { pares: [2, 4, 6], impares: [1, 3, 5] } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function primeirosN(array, n){ | ||
| let primeiros = []; | ||
|
|
||
| for(let i = 0; i < n; i++){ | ||
| primeiros.push(array[i]); | ||
| } | ||
|
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. Loop boundary allows out-of-bounds array accessLow Severity The |
||
|
|
||
| return primeiros; | ||
| } | ||
|
|
||
| console.log(primeirosN([1, 2, 3, 4, 5], 3)); | ||
| console.log(primeirosN(["a", "b", "c", "d"], 2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function removerValor (array, n){ | ||
| let corretos = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] !== n){ | ||
| corretos.push(array[i]) | ||
| } | ||
| } | ||
|
|
||
| return corretos | ||
| } | ||
|
|
||
|
|
||
| console.log(removerValor([1, 2, 3, 2, 4, 2, 5], 2)); | ||
| // Retorna: [1, 3, 4, 5] | ||
|
|
||
| console.log(removerValor(["a", "b", "c", "b"], "b")); | ||
| // Retorna: ["a", "c"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| function somaAcumulada (array){ | ||
| let soma = 0; | ||
| let resultado = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++){ | ||
| soma += array[i]; | ||
| resultado.push(soma) | ||
| } | ||
|
|
||
| return resultado | ||
| } | ||
|
|
||
|
|
||
| console.log(somaAcumulada([1, 2, 3, 4])); | ||
| // [1, 1+2=3, 1+2+3=6, 1+2+3+4=10] | ||
| // Retorna: [1, 3, 6, 10] | ||
|
|
||
| console.log(somaAcumulada([5, 10, 15])); | ||
| // [5, 5+10=15, 5+10+15=30] | ||
| // Retorna: [5, 15, 30] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| function substituir (array, valorAntigo, valorNovo){ | ||
| let novo = []; | ||
|
|
||
| for(let i = 0; i < array.length; i++){ | ||
| if (array[i] !== valorAntigo){ | ||
| novo.push(array[i]) | ||
| } else if (array[i] === valorAntigo){ | ||
| novo.push(valorNovo) | ||
| } | ||
| } | ||
|
|
||
| return novo | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| console.log(substituir([1, 2, 3, 2, 4], 2, 99)); | ||
| // Retorna: [1, 99, 3, 99, 4] | ||
|
|
||
| console.log(substituir(["a", "b", "c", "b"], "b", "z")); | ||
| // Retorna: ["a", "z", "c", "z"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| function todosPositivos(array){ | ||
| for (let i = 0; i < array.length; i++){ | ||
| if (array[i] <= 0){ | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| console.log(todosPositivos([1, 2, 3, 4])); // true ✅ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function contemElemento(array, elemento){ | ||
| for (let i = 0; i < array.length; i++) { | ||
| if (array[i] === elemento) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| console.log(contemElemento([10, 20, 30], 20)); | ||
| console.log(contemElemento([10, 20, 30], 99)); |


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.
Function only counts lowercase vowels, misses uppercase
Low Severity
The
contarVogaisfunction only checks for lowercase vowels (a,e,i,o,u) and ignores uppercase vowels (A,E,I,O,U). This causes incorrect counts when input contains uppercase letters. For example,contarVogais("JavaScript")would return 2 instead of 3 because the capitalAis missed, andcontarVogais("HELLO")would return 0 instead of 2.