From 2d59cf0dd7fc7c9f115482334d9b2edd273441d5 Mon Sep 17 00:00:00 2001 From: Thales Morais Date: Sun, 23 Apr 2023 17:47:35 -0300 Subject: [PATCH] Atividades em JavaScript --- ContandoCaracteres.js | 16 ++++++++++++++++ DeepEquals.js | 18 ++++++++++++++++++ Fizzbuzzdiferente.js | 13 +++++++++++++ FuncaoMatriciais.js | 27 +++++++++++++++++++++++++++ MaximoMinimo.js | 9 +++++++++ Ordenacao.js | 32 ++++++++++++++++++++++++++++++++ Recursividade.js | 5 +++++ RevertendoArray.js | 7 +++++++ Tabuleiroxadrez.js | 13 +++++++++++++ TrabalhandoIntervalos.js | 15 +++++++++++++++ TrabalhandoListas.js | 11 +++++++++++ Triangulo.js | 9 +++++++++ VerificandoPalindromos.js | 12 ++++++++++++ 13 files changed, 187 insertions(+) create mode 100644 ContandoCaracteres.js create mode 100644 DeepEquals.js create mode 100644 Fizzbuzzdiferente.js create mode 100644 FuncaoMatriciais.js create mode 100644 MaximoMinimo.js create mode 100644 Ordenacao.js create mode 100644 Recursividade.js create mode 100644 RevertendoArray.js create mode 100644 Tabuleiroxadrez.js create mode 100644 TrabalhandoIntervalos.js create mode 100644 TrabalhandoListas.js create mode 100644 Triangulo.js create mode 100644 VerificandoPalindromos.js diff --git a/ContandoCaracteres.js b/ContandoCaracteres.js new file mode 100644 index 0000000..e70f625 --- /dev/null +++ b/ContandoCaracteres.js @@ -0,0 +1,16 @@ +const frase = prompt('Digite a frase: '); +const char = prompt('Digite o caractere: '); + +const countChars = (frase, c) => { + let count = 0; + + for (let i = 0; i < frase.length; i++) { + if (frase[i] === c) { + count++; + } + } + + return count; +}; + +console.log(countChars(frase, char)); diff --git a/DeepEquals.js b/DeepEquals.js new file mode 100644 index 0000000..841d7e8 --- /dev/null +++ b/DeepEquals.js @@ -0,0 +1,18 @@ +function deepEquals(obj1, obj2) { + + if (Object.keys(obj1).length !== Object.keys(obj2).length) { + return false; + } + + for (let prop in obj1) { + if (typeof obj1[prop] === "object" && typeof obj2[prop] === "object") { + if (!deepEquals(obj1[prop], obj2[prop])) { + return false; + } + } else if (obj1[prop] !== obj2[prop]) { + return false; + } + } + return true; + } + \ No newline at end of file diff --git a/Fizzbuzzdiferente.js b/Fizzbuzzdiferente.js new file mode 100644 index 0000000..7a299b8 --- /dev/null +++ b/Fizzbuzzdiferente.js @@ -0,0 +1,13 @@ +const limite = 100; + +for (let i = 1; i <= limite; i++) { + if (i % 3 === 0 && i % 5 === 0) { + console.log('FizzBuzz'); + } else if (i % 3 === 0) { + console.log('Fizz'); + } else if (i % 5 === 0) { + console.log('Buzz'); + } else { + console.log(i); + } +} diff --git a/FuncaoMatriciais.js b/FuncaoMatriciais.js new file mode 100644 index 0000000..cda4563 --- /dev/null +++ b/FuncaoMatriciais.js @@ -0,0 +1,27 @@ +function createMatrix(rows, cols, matrixFunc) { + let matrix = new Array(rows); + + for (let i = 0; i < rows; i++) { + matrix[i] = new Array(cols); + + for (let j = 0; j < cols; j++) { + matrix[i][j] = matrixFunc(i, j); + } + } + +let matrix1 = createMatrix(3, 3, (i, j) => i + j); + console.log(matrix1); + +let matrix2 = createMatrix(3, 3, (i, j) => i * j); + console.log(matrix2); + +let matrix3 = createMatrix(3, 3, (i, j) => i == j ? 1 : 0); + console.log(matrix3); + +let matrix4 = createMatrix(3, 3, (i, j) => i**2 / (j+1)); + console.log(matrix4); + +let matrix5 = createMatrix(3, 3, (i, j) => i > j ? 1 : (i < j ? 5 : 0)); + console.log(matrix5); + return matrix; + } \ No newline at end of file diff --git a/MaximoMinimo.js b/MaximoMinimo.js new file mode 100644 index 0000000..cdfb0d1 --- /dev/null +++ b/MaximoMinimo.js @@ -0,0 +1,9 @@ +const numa = parseFloat(prompt('Digite um número: ')); +const numb = parseFloat(prompt('Digite o outro número: ')); + +const min = (a, b) => y < x ? y : x; + +const max = (a, b) => y > x ? y : x; + +console.log(min(numa, numb)); +console.log(max(numa, numb)); diff --git a/Ordenacao.js b/Ordenacao.js new file mode 100644 index 0000000..ab1b35a --- /dev/null +++ b/Ordenacao.js @@ -0,0 +1,32 @@ +function bubbleSort(arr, compareFn) { + const n = arr.length; + let swapped = false; + + do { + swapped = false; + for (let i = 0; i < n - 1; i++) { + if (compareFn(arr[i], arr[i + 1])) { + const temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + swapped = true; + } + } + } while (swapped); + + return arr; + } + function compCresc(a, b) { + return a > b; + } + function compDesc(a, b) { + return a < b; + } + function compCrescAbs(a, b) { + return Math.abs(a) > Math.abs(b); + } + const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]; + +console.log(bubbleSort(arr.slice(), compCresc)); +console.log(bubbleSort(arr.slice(), compDesc)); +console.log(bubbleSort(arr.slice(), compCrescAbs)); diff --git a/Recursividade.js b/Recursividade.js new file mode 100644 index 0000000..29ba31d --- /dev/null +++ b/Recursividade.js @@ -0,0 +1,5 @@ +const num = parseFloat(prompt('Digite o número: ')); + +const mod2 = (num) => num % 2 === 0; + +console.log(mod2(num)); diff --git a/RevertendoArray.js b/RevertendoArray.js new file mode 100644 index 0000000..8be0309 --- /dev/null +++ b/RevertendoArray.js @@ -0,0 +1,7 @@ +function reverse(array) { + list = [1,2,3,4,5] + var arrayRev = array.reverse() + return arrayRev +} + +console.log(reverseArray(list)) \ No newline at end of file diff --git a/Tabuleiroxadrez.js b/Tabuleiroxadrez.js new file mode 100644 index 0000000..6684f44 --- /dev/null +++ b/Tabuleiroxadrez.js @@ -0,0 +1,13 @@ +const linhas = parseInt(prompt('Quantidade de linhas: ')); + +for (let i = 0; i < linhas; i++) { + let linhaAtual = ''; + + if (i % 2 === 0) { + linhaAtual = '# # # # '; + } else { + linhaAtual = ' # # # #'; + } + + console.log(linhaAtual); +} diff --git a/TrabalhandoIntervalos.js b/TrabalhandoIntervalos.js new file mode 100644 index 0000000..e6890bb --- /dev/null +++ b/TrabalhandoIntervalos.js @@ -0,0 +1,15 @@ +var minimo = num(prompt('Número mínimo: ')); +var maximo = num(prompt('Número máximo: ')); +var intervalo = num(prompt('O intervalo: ')); + +function range(min, max, interval) { + var num = []; + + for (let i = min + 1; i < max; i += interval) { + num.push(i); + } + + return num; +} + +console.log(range(minimo, maximo, intervalo)); diff --git a/TrabalhandoListas.js b/TrabalhandoListas.js new file mode 100644 index 0000000..8e8fa1f --- /dev/null +++ b/TrabalhandoListas.js @@ -0,0 +1,11 @@ +function toList(array) { + var list = null; + for (var i = array.length - 1; i >= 0; i--) { + list = {value: array[i], rest: list}; + } + return list; + } + var array = [1, 2, 3]; + var list = toList(array); + console.log(list); + \ No newline at end of file diff --git a/Triangulo.js b/Triangulo.js new file mode 100644 index 0000000..e5b311d --- /dev/null +++ b/Triangulo.js @@ -0,0 +1,9 @@ +const linhas = parseInt(prompt('Quantidade de linhas: ')); + +for (let i = 0; i < linhas; i++) { + let linhaAtual = ''; + for (let j = 0; j <= i; j++) { + linhaAtual += '#'; + } + console.log(linhaAtual); +} diff --git a/VerificandoPalindromos.js b/VerificandoPalindromos.js new file mode 100644 index 0000000..49f2664 --- /dev/null +++ b/VerificandoPalindromos.js @@ -0,0 +1,12 @@ +const palavra = prompt('Digite a palavra: '); + +function isPalindromo(str) { + const strReversa = str.split('').reverse().join(''); + return str === strReversa; +} + +if (isPalindromo(palavra)) { + console.log('É um palíndromo.'); +} else { + console.log('Não é um palíndromo.'); +}