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 / 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 / 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
}
}
@darkcris1
darkcris1 / selectionSort.js
Created January 3, 2021 11:42
Selection Sort Algorithm
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] < arr[j]) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
@darkcris1
darkcris1 / insertionSort.js
Created January 3, 2021 11:43
Insertion Sort Alagorithm
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
for (let j = i; j > 0; j--) {
if (arr[i] < arr[j - 1]) {
const temp = arr[i]
arr[i] = arr[j - 1]
arr[j - 1] = temp
i--
}
}