Skip to content

Instantly share code, notes, and snippets.

View pedroglcbarros's full-sized avatar

Pedro pedroglcbarros

View GitHub Profile
@pedroglcbarros
pedroglcbarros / strCharCount.js
Last active November 17, 2021 22:29
Character Counting Algorithm
function strCharCount (str) {
const chars = {};
for (let char of str) {
chars[char] = 0;
}
for (let char of str) {
if(Object.keys(chars).includes(char)) {
@pedroglcbarros
pedroglcbarros / isAnagram.js
Created November 16, 2021 17:09
Anagram Algorithm
function isAnagram (word1, word2) {
return word1.toLowerCase().split("").sort().join("") === word2.toLowerCase().split("").sort().join("")
}
@pedroglcbarros
pedroglcbarros / isPalindrome.js
Last active November 17, 2021 22:34
Palindrome Algorithm
function isPalindrome (word) {
invertedString = [];
for (let i = word.length - 1; i >= 0; i--) {
invertedString.push(word[i])
}
return word.toLowerCase() === invertedString.join("").toLowerCase();
@pedroglcbarros
pedroglcbarros / sequentialSearch.js
Last active November 17, 2021 22:33
Sequential Search Algorithm
function sequentialSearch (array, query) {
isItemFound = false;
for (let i = 0; i < array.length; i++) {
if (array[i] === query) return true;
}
return isItemFound;
}
@pedroglcbarros
pedroglcbarros / fizzBuzz.js
Last active November 16, 2021 13:11
FizzBuzz Algorithm
function fizzBuzz () {
for (let i = 1; i <= 100; 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)
@pedroglcbarros
pedroglcbarros / countdown.js
Last active September 26, 2021 12:45
Function to countdown the time between two dates.
function countdown(targetDate) {
const now = new Date();
const future = new Date(targetDate)
const feb29 = new Date(`February 29 ${future.getFullYear()} 00:00:00`);
const isLeapYear = feb29.getDate() === 29 ? true : false;
const years = future.getFullYear() - now.getFullYear();
const months = (future.getMonth() + 1) - (now.getMonth() + 1) >= 0 ?
@pedroglcbarros
pedroglcbarros / bingo.js
Last active October 6, 2021 18:15
Function to generate random numbers for a bingo game
const nums = [];
function bingo() {
let draw = Math.ceil(Math.random() * 75);
nums.includes(draw) ? bingo() : nums.push(draw);
return nums;
}
@pedroglcbarros
pedroglcbarros / passwordGenerator.js
Last active October 29, 2021 14:04
Function to generate password
// USING ARRAY
function generatePassword(passwordLength) {
const components = [
["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"],
["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"],
[0,1,2,3,4,5,6,7,8,9],
["!", "#", "$", "%", "&", "/", "*", "~", "^", "+", "-",".", "?", "@", "(", ")","[", "]", "{", "}",":", ";"]
]
const password = [];
@pedroglcbarros
pedroglcbarros / invertString.js
Last active November 17, 2021 22:36
String Inversion Algorithm
function invertString (str) {
invertedString = [];
for (let i = str.length - 1; i >= 0; i--) {
invertedString.push(str[i])
}
return invertedString.join("")
}