Skip to content

Instantly share code, notes, and snippets.

View darkcris1's full-sized avatar
🎯
Focusing

Cris Fandiño Jr. darkcris1

🎯
Focusing
  • Philippines
View GitHub Profile
@darkcris1
darkcris1 / generateFilePaths.js
Last active January 1, 2021 11:02
Generate a file paths in next.js | This is useful if you are using getStaticPaths
import fs from 'fs'
const ignoreRegex = /_(.*?).md/
const isMd = (value) => /\.md$/.test(value)
function generateMdPaths(data, currentPath = '', ext = '') {
return data.reduce((acc, value) => {
if (ignoreRegex.test(value)) return acc
if (!isMd(value)) {
const path = `${currentPath}/${value}/`
@darkcris1
darkcris1 / passwordgenerator.js
Last active February 7, 2021 10:48
Simple password generator
function generatePassword(length = 50) {
if (length < 5) throw new Error('Length must be minimum of 5')
const chars = 'abcdefghijklmopqrstnuvwxyz-[]{}()*-+,./\\1234567890'
let result = ''
for (let i = 0; i < length; i++) {
const randomChars = chars[Math.floor(Math.random() * chars.length)]
result += randomChars
}
return result
@darkcris1
darkcris1 / mergeSort.js
Created January 3, 2021 11:06
Merge Sort Algorithm using javascript
function sort(arrL, arrR) {
const newArray = []
while (arrL.length && arrR.length) {
arrR[0] < arrL[0]
? newArray.push(arrR.shift())
: newArray.push(arrL.shift())
}
return newArray.concat(arrL, arrR)
}
@darkcris1
darkcris1 / longPrefix.js
Created January 3, 2021 11:12
match a longprefix
function longPrefix(arr) {
let result = ''
if (arr.length === 0) return result
for (let i = 0; i < arr[0].length || 0; i++) {
for (let j = 1; j < arr.length; j++) {
if (arr[0][i] === arr[j][i]) {
if (j + 1 === arr.length) {
result += arr[0][i]
}
} else {
@darkcris1
darkcris1 / snailsort algorithm
Created January 3, 2021 11:15
Snailsort Algorithm | Codewars
const snailSort = (arr) => {
let result = []
while (arr.length) {
if (arr.length === 1) {
return [...result, ...arr[0]]
}
for (let i = 0; i < arr[0].length; i++) {
result.push(arr[0][i])
}
arr.shift()
@darkcris1
darkcris1 / scrabblePoints.js
Created January 3, 2021 11:17
Scrabble points solution | Codewars
function scrabblePoints(str) {
const strs = str.split(' ')
let letterPoints = 0
const totalPoints = strs.reduce(
(acc, value) => {
if (value.length > 10 || !value) return acc
console.log(value)
value
.trim()
.toLowerCase()
@darkcris1
darkcris1 / solveExpression.js
Created January 3, 2021 11:23
Find the unknown digit | Codewars
function solveExpression(exp) {
for (let i = 0; i <= 9; i++) {
const ex = exp.replace(/\?/g, i).split('=')
if (/--/.test(ex[0])) {
ex[0] = ex[0].replace(/(-([0-9]+))/g, '($1)')
}
if (/\b00|00\b/g.test(ex.join('='))) continue
if (exp.includes(i)) continue
if (eval(ex[0]) == ex[1]) return i
}
@darkcris1
darkcris1 / strangeRoot.js
Created January 3, 2021 11:28
Strange Root | SoloLearn
function strangeRoot(n) {
const sqrt = Math.sqrt(n)
.toFixed(3)
.replace(/\.(0{3})$/, '')
return new RegExp(n * n).test(sqrt)
}
console.log(strangeRoot(2)) // true
// Strange Root Visualize
@darkcris1
darkcris1 / nBonacci.js
Created January 3, 2021 11:38
n-bonacci algorithm | SoloLearn
const nFibonacci = (n, max) => {
const result = Array(n)
.fill(0)
.fill(1, n - 1 || 1)
for (let i = result.length; i < max; i++) {
const nSums = result
.slice(result.length - n)
.reduce((acc, val) => BigInt(acc) + BigInt(val), 0)
result.push(nSums)
}
@darkcris1
darkcris1 / bubbleSort.js
Created January 3, 2021 11:41
Bubble Sort Algorithm
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 1; j < arr.length; j++) {
if (arr[j] < arr[j - 1]) {
const temp = arr[j - 1]
arr[j - 1] = arr[j]
arr[j] = temp
}
}