diff --git a/bubbleSort.js b/bubbleSort.js new file mode 100644 index 0000000..e8ca6a2 --- /dev/null +++ b/bubbleSort.js @@ -0,0 +1,45 @@ +const array = [7, 2, 4, 1, 6, 1, 5, 8]; + +function bubbleSort(array, func){ + for(let i = 0; i < array.length-1; i++){ + for(let j = 0; j < array.length-1; j++){ + if(func(array[j], array[j+1]) > 0){ + [array[j], array[j+1]] = [array[j+1], array[j]]; + } + } + } + return array; +} + +function crescente(a, b){ + return a - b; +} + +function decrescente(a, b){ + return b - a; +} + +function crescImpar(a, b){ + if((a % 2 === 1) && (b % 2 === 0)){ + return -1; + } else if((a % 2 === 0) && (b % 2 === 1)){ + return 1; + } else { + return a - b; + } +} + +function decrescPar(a, b){ + if ((a % 2 === 0) && (b % 2 === 1)) { + return -1; + } else if ((a % 2 === 1) && (b % 2 === 0)) { + return 1; + } else { + return b - a; + } +} + +console.log(bubbleSort(array, crescente)); +console.log(bubbleSort(array, decrescente)); +console.log(bubbleSort(array, crescImpar)); +console.log(bubbleSort(array, decrescPar)); diff --git a/chess.js b/chess.js new file mode 100644 index 0000000..659c47c --- /dev/null +++ b/chess.js @@ -0,0 +1,24 @@ +let numLinhas = parseInt(prompt("Quantas linhas?")); + +for(let i = 0; i < numLinhas; i++){ + let line=""; + if(i % 2 == 0){ + for(let j = 0; j < numLinhas; j++){ + if(j % 2 == 0){ + line+= "#"; + } else { + line+= " "; + } + } + } else { + for(let j = 0; j < numLinhas; j++){ + if(j % 2 == 0){ + line+= " "; + } else { + line+= "#"; + } + } + } + console.log(line); + +} \ No newline at end of file diff --git a/cifraCesar.js b/cifraCesar.js new file mode 100644 index 0000000..e5113b9 --- /dev/null +++ b/cifraCesar.js @@ -0,0 +1,25 @@ +const alfabeto = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; +function cifraCesar(string, skip){ + let cifra = ''; + let achou = false; + string = string.toLowerCase(); + + for(let i = 0; i < string.length; i++){ + for(let j = 0; j < alfabeto.length; j++){ + if(string[i] == alfabeto[j]){ + cifra += alfabeto[(j + skip) % 26]; + achou = true; + } + } + if(achou == 0){ + cifra += string[i]; + } + achou = 0; + } + return cifra; +} + +const string = "Hello World!"; +console.log(cifraCesar(string, 1)); +console.log(cifraCesar(string, 0)); +console.log(cifraCesar(string, 5)); diff --git a/countChar.js b/countChar.js new file mode 100644 index 0000000..c3c342c --- /dev/null +++ b/countChar.js @@ -0,0 +1,8 @@ +function countChars(frase, c){ + return frase.split(c).length - 1; +} + +let frase = prompt("Qual palavra?"); +let c = prompt("Char?"); + +console.log(countChars(frase, c)); \ No newline at end of file diff --git a/deepEquals.js b/deepEquals.js new file mode 100644 index 0000000..c663cfb --- /dev/null +++ b/deepEquals.js @@ -0,0 +1,41 @@ +function deepEquals(obj1, obj2){ + let objK1 = Object.keys(obj1); + let objK2 = Object.keys(obj2); + + if (objK1.length !== objK2.length) return false; + + for(let key of objK1){ + let v1 = obj1[key]; + let v2 = obj2[key]; + + let isObj = isObject(v1) && isObject(v2); + if((isObj && !deepEquals(v1, v2)) || (!isObj && (v1 !== v2))) return false; + } + + return true; +} + +function isObject(obj){ + return obj!=null && typeof obj === "object"; +} + +const obj1 = { + "nome" : "smola", + "matricula" : "2021951450", + "idade" : "17", +}; + +const obj2 = { + "nome" : "leo", + "matricula" : "2021951566", + "idade" : "17", +}; + +const obj3 = { + "nome" : "smola", + "matricula" : "2021951450", + "idade" : "17", +}; + +console.log(deepEquals(obj1, obj3)); +console.log(deepEquals(obj1, obj2)); \ No newline at end of file diff --git a/div35.js b/div35.js new file mode 100644 index 0000000..227eac2 --- /dev/null +++ b/div35.js @@ -0,0 +1,17 @@ +for(let i = 0; i < 100; i++){ + let line = ''; + + if (i % 3 == 0){ + line+= "Fizz"; + } + if (i % 5 == 0){ + line+= "Buzz"; + } + + if (line === ''){ + console.log(i); + } else{ + console.log(line); + } + +} \ No newline at end of file diff --git a/linkedList.js b/linkedList.js new file mode 100644 index 0000000..8874a2c --- /dev/null +++ b/linkedList.js @@ -0,0 +1,30 @@ +class List { + constructor(value, rest){ + this.value = value; + this.rest = rest; + } + + print(){ + let line = this.value ; + let p = this.rest; + while(p){ + line += ", " + p.value; + p = p.rest; + } + console.log(line); + } +} + +function toList(array){ + rest = null; + for(let i = 0; i < array.length; i++){ + var list = new List(array[i], rest); + rest = list; + } + return list; +} + +let array = [1, 2, 3, 4, 5]; +let list = toList(array); + +list.print(); \ No newline at end of file diff --git a/matriz.js b/matriz.js new file mode 100644 index 0000000..df2df82 --- /dev/null +++ b/matriz.js @@ -0,0 +1,40 @@ +function criaMatriz(tam, func){ + let matriz = []; + for(let i = 1; i <= tam; i++){ + let linha = []; + for(let j = 1; j <= tam; j++){ + linha.push(func(i, j)); + } + matriz.push(linha); + } + return matriz; +} + +function func1(i, j){ + return i + j +} + +function func2(i, j){ + return i * j; +} + +function func3(i, j){ + if (i == j) return 1; + else return 0; +} + +function func4(i, j){ + return (i*i)/(j+1); +} + +function func5(i, j){ + if(i > j) return 1; + else if (i < j) return 5; + else return 0; +} + +console.log(criaMatriz(5, func1)); +console.log(criaMatriz(5, func2)); +console.log(criaMatriz(5, func3)); +console.log(criaMatriz(5, func4)); +console.log(criaMatriz(5, func5)); diff --git a/minmax.js b/minmax.js new file mode 100644 index 0000000..3c7e94a --- /dev/null +++ b/minmax.js @@ -0,0 +1,25 @@ +function min(a, b){ + if (a < b){ + return a; + } else if(b < a){ + return b; + } else { + return "Igual"; + } +} + +function max(a, b){ + if(a>b){ + return a; + } else if(b>a){ + return b; + } else{ + return "Igual"; + } +} + +let a = parseInt(prompt("a?")); +let b = parseInt(prompt("b?")); + +console.log(min(a, b)); +console.log(max(a, b)); diff --git a/mod.js b/mod.js new file mode 100644 index 0000000..e93aca9 --- /dev/null +++ b/mod.js @@ -0,0 +1,28 @@ +function mod2(number){ + if(number >= 2){ + return mod2((number-2)); + } else if(number == 0){ + return true; + } else { + return false; + } +} + +function mod(num, modu){ + if(num >= modu){ + return mod((num-modu), modu); + } else if(num == 0){ + return true; + } else { + return false; + } +} + +let num = parseInt(prompt("num")); +let modu = parseInt(prompt("mod")); + +if (mod(num,modu)){ + console.log("É divisível"); +} else { + console.log("Não é divisível"); +} diff --git a/numCheck.js b/numCheck.js new file mode 100644 index 0000000..fc1f3e1 --- /dev/null +++ b/numCheck.js @@ -0,0 +1,20 @@ +function numCheck(num, func){ + return func(num); +} + +function ehImpar(num){ + return num % 2 === 1; +} + +function ehPrimo(num){ + if (num <= 1) return false; + for(let i = 2; i < num/2; i++){ + if(num % i === 0) return false; + } + return true; +} + +console.log(numCheck(3, ehImpar)); +console.log(numCheck(2, ehImpar)); +console.log(numCheck(3, ehPrimo)); +console.log(numCheck(2, ehPrimo)); diff --git a/palindrome.js b/palindrome.js new file mode 100644 index 0000000..68845a6 --- /dev/null +++ b/palindrome.js @@ -0,0 +1,9 @@ +let word = prompt("Qual palavra?"); + +word = word.toLowerCase(); +let drow = word.split('').reverse().join(''); +if (drow === word){ + console.log("Palíndromo"); +} else { + console.log("Não palíndromo"); +} \ No newline at end of file diff --git a/piramid.js b/piramid.js new file mode 100644 index 0000000..9631c69 --- /dev/null +++ b/piramid.js @@ -0,0 +1,9 @@ +let numLinhas = parseInt(prompt("Quantas linhas?")); + +for(let i = 0; i < numLinhas; i++){ + let line = "#"; + for(let j = 0; j<=i; j++){ + line+="#"; + } + console.log(line); +} \ No newline at end of file diff --git a/range.js b/range.js new file mode 100644 index 0000000..4245323 --- /dev/null +++ b/range.js @@ -0,0 +1,14 @@ +function range(min, max, i){ + let array = []; + for(let j = min; j <=max; j+=i){ + array.push(j); + } + return array; +} + + +let min = parseInt(prompt("min")); +let max = parseInt(prompt("max")); +let i = parseInt(prompt("i")); + +console.log(range(min,max,i)); \ No newline at end of file diff --git a/reverseArray.js b/reverseArray.js new file mode 100644 index 0000000..71e1a82 --- /dev/null +++ b/reverseArray.js @@ -0,0 +1,10 @@ +function reverseArray(array){ + let reversed = []; + for(let i = 0; i < array.length; i++){ + reversed.push(parseInt(array[array.length - (i + 1)])); + } + return reversed; +} + +let array = [1, 2, 3, 4, 5]; +console.log(reverseArray(array)); \ No newline at end of file diff --git a/stringTransform.js b/stringTransform.js new file mode 100644 index 0000000..93addb2 --- /dev/null +++ b/stringTransform.js @@ -0,0 +1,34 @@ +function stringTransform(string, func){ + let transform = ''; + for(let i = 0; i < string.length; i++){ + transform += func(string[i]); + } + return transform; +} + +function vogalMaiusc(char){ + if(/[aeiou]/i.test(char)) return char.toUpperCase(); + else return char; +} + +function consoanteMaiusc(char){ + if(/[bcdfghjklmnpqrstvwxyz]/i.test(char)) return char.toUpperCase(); + else return char; +} + +function vogalMinusc(char){ + if(/[aeiou]/i.test(char)) return char.toLowerCase(); + else return char; +} + +function consoanteMinusc(char){ + if(/[bcdfghjklmnpqrstvwxyz]/i.test(char)) return char.toLowerCase(); + else return char; +} + +let string = "Hello Earth!"; + +console.log(stringTransform(string, vogalMaiusc)); +console.log(stringTransform(string, consoanteMaiusc)); +console.log(stringTransform(string, vogalMinusc)); +console.log(stringTransform(string, consoanteMinusc));