Skip to content

Instantly share code, notes, and snippets.

View squalvj's full-sized avatar
24/7

Aditya Wiraha squalvj

24/7
  • Jakarta
View GitHub Profile
@squalvj
squalvj / regex.js
Last active February 15, 2024 07:39
This is a ungreedy solution in regex javascript
/(.*?)/g
// (.*?) is ungreedy
@squalvj
squalvj / quicksort.js
Created April 29, 2020 09:22
Super quicksort algorithm
const quickSort = list => {
if (list.length < 2)
return list;
let pivot = list[0];
let left = [];
let right = [];
for (let i = 1, total = list.length; i < total; i++){
if (list[i] < pivot)
left.push(list[i]);
else
@squalvj
squalvj / checkVisible.js
Created April 16, 2020 07:22
Check element is visible in viewport or no
// assuming el is dom object
function checkVisible(el)
{
return el.offsetTop < window.innerHeight + window.pageYOffset
}
@squalvj
squalvj / time.js
Created April 15, 2020 04:25
Minute, Second, Hour Calculation
const time = 3600 //second 1 hour
var minutes = Math.floor(time / 60);
//And to get the remaining seconds, multiply the full minutes with 60 and subtract from the total seconds:
var seconds = time - minutes * 60;
//Now if you also want to get the full hours too, divide the number of total seconds by 3600 (60 minutes/hour · 60 seconds/minute) first, then calculate the remaining seconds:
var hours = Math.floor(time / 3600);
@squalvj
squalvj / fade.js
Created July 26, 2019 06:39
Animation Fade out using font-size 0
import React, { useState } from "react";
import ReactDOM from "react-dom";
import { Transition } from "react-transition-group";
import "./styles.css";
function App() {
const [visible, setVisible] = useState(true);
return (
@squalvj
squalvj / fibonacci.js
Created July 25, 2019 05:22
Fibonacci on js
function fibonacci(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
@squalvj
squalvj / wow.test.js
Created July 24, 2019 10:30
Test manipulating state with jest
mport React from 'react';
import { shallow, mount, render } from 'enzyme';
import Todo from '../components/Todo';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
test('Test it', async () => {
@squalvj
squalvj / JavascriptTruthyness.js
Created June 3, 2019 06:29
Javascript Truthiness
1 == 1 //->true
1 == "1" //->true
1 == true//-> true
1 == 0 //-> false
1 == [] //-> false
@squalvj
squalvj / formula.js
Created January 8, 2019 14:55
Algorithm calculate third number based on min max and convert them to percent
// basic
var max = 250
var min = 50
var input = 65
var percent = ((input - min) * 100) / (max - min)
// a little longer
var range = max - min
var correctedStartValue = input - min
var percentage = (correctedStartValue * 100) / range
@squalvj
squalvj / takeString.js
Created January 2, 2019 06:10
Take the last string using split and splice
const url = 'auth/user/ucok';
const lastStr = url.split("/").slice(-1)[0];
// output 'ucok'