diff --git a/changeString.js b/changeString.js new file mode 100644 index 0000000..b00b01a --- /dev/null +++ b/changeString.js @@ -0,0 +1,91 @@ +var minha_frase = "uma frase toda minuscula para testes"; +var minha_frase2 = "OUTRA FRASE SO QUE TODA MAIUSCULA PARA TESTES"; + +function upper(array, char){ + + var tmp = array; + if(array[0] == char){ + tmp = array[0].toUpperCase() + array.slice(1); + }else{ + for(i = 1; i < array.length; i++){ + if(array[i] == char){ + tmp = array.slice(0, i) + array[i].toUpperCase() + array.slice(++i); + break; + } + } + } + return tmp; +} + +function lower(array, char){ + + var tmp = array; + if(array[0] == char){ + tmp = array[0].toLowerCase() + array.slice(1); + }else{ + for(i = 1; i < array.length; i++){ + if(array[i] == char){ + tmp = array.slice(0, i) + array[i].toLowerCase() + array.slice(++i); + break; + } + } + } + return tmp; +} + +function upperVogais(old){ + + var vogais = "aeiou"; + var novo = old; + for(j = 0; j < vogais.length; j++){ + for(i = 0; i < old.length; i++){ + novo = upper(novo, vogais[j]); + } + } + return novo; +} + +function lowerVogais(old){ + + var vogais = "AEIOU"; + var novo = old; + for(j = 0; j < vogais.length; j++){ + for(i = 0; i < old.length; i++){ + novo = lower(novo, vogais[j]); + } + } + return novo; +} + +function upperConsoantes(old){ + + var consoantes = "bcdfghjklmnpqrstvxyz"; + var novo = old; + for(j = 0; j < consoantes.length; j++){ + for(i = 0; i < old.length; i++){ + novo = upper(novo, consoantes[j]); + } + } + return novo; +} + +function lowerConsoantes(old){ + + var consoantes = "BCDFGHJKLMNPQRSTVXYZ"; + var novo = old; + for(j = 0; j < consoantes.length; j++){ + for(i = 0; i < old.length; i++){ + novo = lower(novo, consoantes[j]); + } + } + return novo; +} + +function changeString(frase, condicao){ + return condicao(frase); +} + +console.log("Vogais maiusculas - " + changeString(minha_frase, upperVogais)); +console.log("Vogais minusculas - " + changeString(minha_frase2, lowerVogais) + "\n"); +console.log("Consoantes maiusculas - " + changeString(minha_frase, upperConsoantes)); +console.log("Consoantes minusculas - " + changeString(minha_frase2, lowerConsoantes)); diff --git a/countChars.js b/countChars.js new file mode 100644 index 0000000..7b5489b --- /dev/null +++ b/countChars.js @@ -0,0 +1,15 @@ +var palavra = prompt("Digite uma palavra ou frase - ") +var char = prompt("Digite um caractere - "); + +function countChars(frase, c){ + + var quant = 0; + for(i = 0; i < frase.length; i++){ + if(frase[i] == c){ + quant++; + } + } + return quant; +} + +console.log("Seu caractere apareceu " + countChars(palavra, char) + " vezes"); \ No newline at end of file diff --git a/criptografia.js b/criptografia.js new file mode 100644 index 0000000..808b229 --- /dev/null +++ b/criptografia.js @@ -0,0 +1,29 @@ +var minha_frase = "Uma frase para fim de testes muahahahahha" +var td = "abcdefghijklmnopqrstuvwxyz"; + +function cifraCesar(frase, key){ + + var novo = []; + var aux; + var tmp = frase.toLowerCase(); + + for(i = 0; i < tmp.length; i++){ + for(j = 0; j < td.length; j++){ + if(tmp[i] == td[j]){ + aux = j + key; + if(aux >= td.length){ + aux = aux - td.length; + } + novo[i] = td[aux]; + break; + } + } + } + return novo.join(""); +} + +function criptografia(frase, criterio){ + return criterio(frase, 3); +} + +console.log(criptografia(minha_frase, cifraCesar)); \ No newline at end of file diff --git a/deepEquals.js b/deepEquals.js new file mode 100644 index 0000000..fe1ae74 --- /dev/null +++ b/deepEquals.js @@ -0,0 +1,30 @@ +var meu_obj1 = { + num: 1, + nome: "eu", + haha: null +} +var meu_obj2 = { + num: 2, + nome: "voce", + haha: null + //jaja: null +} + +function deepEquals(obj1, obj2){ + + var tmp1 = []; + for (var prop in obj1){ + tmp1.push(prop); + } + var tmp2 = []; + for (var prop in obj2){ + tmp2.push(prop); + } + if(JSON.stringify(tmp1) == JSON.stringify(tmp2)){ + return true; + }else{ + return false; + } +} + +console.log("Os objetos sao iguais? - " + deepEquals(meu_obj1, meu_obj2)); \ No newline at end of file diff --git a/intervalos.js b/intervalos.js new file mode 100644 index 0000000..c153485 --- /dev/null +++ b/intervalos.js @@ -0,0 +1,43 @@ +var num1 = 14; +var num2 = 132; +var variacao = 4; + +function range(min, max){ + + var aux = 0; + if(min > max){ + aux = min; + min = max; + max = aux; + } + + var array = []; + aux = max - min; + for(i = 0; i <= aux ; i++){ + array[i] = min++; + } + return array; +} + +function range2(min, max, i){ + + var aux = 0; + if(min > max){ + + aux = min; + min = max; + max = aux; + } + + var array = []; + for(j = 0; j <= max ; j++){ + array[j] = min; + min += i; + if(min > max) + { break; } + } + return array; +} + +console.log(range(num1, num2)); +console.log(range2(num1, num2, variacao)); \ No newline at end of file diff --git a/matriz.js b/matriz.js new file mode 100644 index 0000000..37b1145 --- /dev/null +++ b/matriz.js @@ -0,0 +1,44 @@ +function criaMatriz(linhas, colunas, funcao){ + + var matriz = []; + for(i = 0; i < linhas; i++){ + var tmp = []; + for(j = 0; j < colunas; j++){ + tmp[j] = funcao((i + 1), (j + 1)); + } + matriz[i] = tmp; + } + + return matriz; +} + +function funcao1(i, j){ + return i + j; +} + +function funcao2(i, j){ + return i * j; +} + +function funcao3(i, j){ + return i == j ? 1 : 0; +} + +function funcao4(i, j){ + return i**2/(j+1); +} + +function funcao5(i, j){ + return i > j ? 1 : (i < j ? 5 : 0); +} + +console.log("Funcao 1"); +console.log(criaMatriz(5, 5, funcao1)); +console.log("Funcao 2"); +console.log(criaMatriz(5, 5, funcao2)); +console.log("Funcao 3"); +console.log(criaMatriz(5, 5, funcao3)); +console.log("Funcao 4"); +console.log(criaMatriz(5, 5, funcao4)); +console.log("Funcao 5"); +console.log(criaMatriz(5, 5, funcao5)); \ No newline at end of file diff --git a/maxEmin.js b/maxEmin.js new file mode 100644 index 0000000..a06970c --- /dev/null +++ b/maxEmin.js @@ -0,0 +1,21 @@ +var num1 = prompt("Digite o primeiro valor - "); +var num2 = prompt("Digite o segundo valor - "); + +function min(a, b){ + if(a < b){ + return a; + }else{ + return b; + } +} + +function max(a, b){ + if(a < b){ + return b; + }else{ + return a; + } +} + +console.log("min - "+ min(num1, num2)); +console.log("max - "+ max(num1, num2)); \ No newline at end of file diff --git a/ordena.js b/ordena.js new file mode 100644 index 0000000..e5318f9 --- /dev/null +++ b/ordena.js @@ -0,0 +1,47 @@ +var meu_array = [3, 5, 1, 10, 4, 2, 6]; + +//bubble sort +function ordena(array, condicao){ + + for(i = 0; i < array.length; i++){ + for(j = 0; j < array.length - 1; j++){ + if(condicao(array[j], array[j + 1])){ + var tmp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = tmp; + } + } + } + return array; +} + +function par_impar(array, aux){ + var novo = []; + var j = 0; + //aux = true - numeros pares + //aux = false - numeros impares + for(i = 0; i < array.length; i++){ + if(((array[i] % 2) == 0 && aux) || ((array[i] % 2) != 0 && !aux) ){ + novo[j] = array[i]; + j++; + } + } + return novo; +} +//crescente +console.log(ordena(meu_array, function(a, b){ + return (a > b); +})); +//decrescente +console.log(ordena(meu_array, function(a, b){ + return (a < b); +})) + +//impar crescente +console.log(ordena(par_impar(meu_array, false), function(a, b){ + return (a > b); +})); +//par decrescente +console.log(ordena(par_impar(meu_array, true), function(a, b){ + return (a < b); +})); \ No newline at end of file diff --git a/palindromo.js b/palindromo.js new file mode 100644 index 0000000..b4258d6 --- /dev/null +++ b/palindromo.js @@ -0,0 +1,19 @@ +var palavra = prompt("Digite sua palavra - "); + +var tam = palavra.length - 1; +var checa = true; + +for(i = 0; i < palavra.length; i++){ + if(palavra[i] == palavra[tam]){ + tam--; + }else{ + checa = false; + break; + } +} + +if(checa == true){ + console.log("É um palíndromo! "); +}else{ + console.log("Não é um palíndromo...") +} \ No newline at end of file diff --git a/programaDiferente.js b/programaDiferente.js new file mode 100644 index 0000000..7f26a65 --- /dev/null +++ b/programaDiferente.js @@ -0,0 +1,23 @@ +var c = 0; +var msg = ""; + +for(i = 1; i <= 100; i++){ + + var a = i.toString(); + for(j = 0; j < a.length; j++){ + c += a[j]; + } + + if((c % 3) == 0){ + msg += "Fizz" + } + + if ((c % 5) == 0){ + msg += "Buzz"; + } + + console.log(i + msg); + + c = 0; + msg = " "; +} \ No newline at end of file diff --git a/recursividade.js b/recursividade.js new file mode 100644 index 0000000..abd458d --- /dev/null +++ b/recursividade.js @@ -0,0 +1,25 @@ +var num1 = 82; +var num2 = 7; + +function mod2(number){ + if((number % 2) == 0){ + return true; + }else{ + return false; + } +} + +function mod(num, modx){ + + if(num >= modx){ + return mod(num - modx, modx); + } + if(num == 0){ + return true; + }else{ + return false; + } +} + +console.log("Divisilvel por 2? - " + mod2(num1)); +console.log("Divisilvel por " + num2 + "? - " + mod(num1, num2)); \ No newline at end of file diff --git a/reverseArray.js b/reverseArray.js new file mode 100644 index 0000000..d5a3786 --- /dev/null +++ b/reverseArray.js @@ -0,0 +1,12 @@ +var meu_array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] + +function reverseArray(array){ + + var reverso = []; + for(i = 1; i <= array.length; i++){ + reverso.push(array[array.length - i]); + } + return reverso; +} + +console.log(reverseArray(meu_array)); \ No newline at end of file diff --git a/toList.js b/toList.js new file mode 100644 index 0000000..fe3cc8d --- /dev/null +++ b/toList.js @@ -0,0 +1,20 @@ +var meu_array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; + +function makeRest(array, i){ + if(i < array.length){ + var tmp = { + value: array[i], + rest: makeRest(array, ++i) + }; + return tmp; + }else{ + return null; + } +} + +function toList(array){ + var list = makeRest(array, 0); + return list; +} + +console.log(toList(meu_array)); \ No newline at end of file diff --git a/triangulo.js b/triangulo.js new file mode 100644 index 0000000..ae5457d --- /dev/null +++ b/triangulo.js @@ -0,0 +1,11 @@ +var tam = prompt("Digite o tamanho do seu triângulo - "); +var velha = " "; + + for(i = 0; i < tam; i++){ + for(j = 0; j <= i; j++){ + velha += "#" + } + velha += "\n"; + } + +console.log(velha); \ No newline at end of file diff --git a/verificaNum.js b/verificaNum.js new file mode 100644 index 0000000..c8021fa --- /dev/null +++ b/verificaNum.js @@ -0,0 +1,35 @@ +var meu_num = 13; + +function forNumber(num, condicao){ + return condicao(num); +} + +function verificaImpar(num){ + return !(num % 2) == 0; +} + +/*Verifica se um número é ímpar +console.log(forNumber(meu_num, function(a){ + return !(a % 2) == 0; +}));*/ +console.log(forNumber(meu_num, verificaImpar)); + +//Verifica se o número é primo +console.log(forNumber(meu_num, function(a){ + + var num; + if(a == 1) + { return true; } + + if(verificaImpar(a)) + { num = (a - 1)/2; } + else + { num = a/2; } + + for(i = 2; i <= num; i++){ + if((a % i) == 0){ + return false; + } + } + return true; + })); \ No newline at end of file diff --git a/xadrez.js b/xadrez.js new file mode 100644 index 0000000..59c54f4 --- /dev/null +++ b/xadrez.js @@ -0,0 +1,15 @@ +var tam = prompt("Digite o tamanho do seu xadrez - "); +var xadrez = ""; + +for (i = 0; i < tam; i++) { + for (j = 0; j < 8; j++) { + if ((i + j) % 2 == 0) { + xadrez += "#"; + } else { + xadrez += " "; + } + } + xadrez += "\n"; +} + +console.log(xadrez); \ No newline at end of file