Skip to content

Instantly share code, notes, and snippets.

View Risyandi's full-sized avatar
🌴
Everyday is a vacation

Risyandi Risyandi

🌴
Everyday is a vacation
View GitHub Profile
@Risyandi
Risyandi / calculateTimeLeft.js
Created February 7, 2023 05:09
show you how to calculate time left to given date.
const now = new Date().getTime(); // current date
const futureDate = new Date('7 Feb 2023 16:40:00').getTime(); // setup time left
const timeleft = futureDate - now;
const days = Math.floor( timeleft / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeleft % (1000 * 60)) / 1000);
@Risyandi
Risyandi / timeLeftCalculate.js
Created February 7, 2023 05:12
calculate the difference between current date (now) and given date.
/** step by step
* 1. Get time that passed since Unix Epoch using Date() constructor with getTime(),
* 2. calculate the difference between current date and future date,
* 3. convert milliseconds to seconds, minutes, hours, etc.
* 4. calculate remaining time using modulo (%) operations and rounding the result using Math.floor() to get approximate result.
**/
const now = new Date().getTime(); // current date
const futureDate = new Date('27 Jan 2030 16:40:00').getTime(); // setup time left
function stockPicker(arr) {
// console.log(arr, "log risyandi: data array");
let maxProfit = -1;
for (let index = 0; index < arr.length; index++) {
for (let jindex = index + 1; jindex < arr.length; jindex++) {
let profit = arr[jindex] - arr[index]
console.log(profit, "log risyandi: profit");
if (profit > maxProfit) {
maxProfit = profit
// console.log(maxProfit, "log risyandi: maxProfit");
@Risyandi
Risyandi / palindromeTwo.js
Created January 17, 2023 04:01
check have a palindrome
let string = "A man, a plan, a canal: Panama";
function palindromeTwo(alphabet) {
alphabet = alphabet.replace(/[^a-z0-9]/gi, "").toLowerCase();
let reverse = alphabet.split("").reverse().join("");
return alphabet === reverse;
}
palindromeTwo(string);
function cekFunction(value) {
let length = value.split(" ").length;
let dimension = "";
if (length <= 2) {
dimension = value.split(" ").length > 1 ? value.split(" ")[0] : "";
} else {
dimension = value.split(" ").length > 1 ? value.split(" ")[1] : "";
}
let dimensionValue = dimension.split("x").length > 1 ? dimension.split("x") : [];
let volume = dimensionValue.length > 1 ? parseFloat(dimensionValue[0]) * parseFloat(dimensionValue[1]) * parseFloat(dimensionValue[2]) : 0;
@Risyandi
Risyandi / getNumberTriple.js
Last active June 23, 2022 15:37
how to get number triple using looping for.
/**
* Risyandi - 2021
* how to create increment to triple number.
* first we have value from looping with initial duplicate 3.
* and the expected result like this :
* 111 222 333
* 111 222 333
* 111 222 333
* 444 555 666
* 444 555 666
@Risyandi
Risyandi / stringEncode.js
Created June 23, 2022 15:31
code challenge to encode a string
/**
* Risyandi - 2022
* how to create string to encode :
* first we have a data array as input
* for the example : [a, a, b, b, b, c, c]
* and for the expected result should be appear like this
* for the expected result : a2b3c2
*/
function stringEncode(input) {
@Risyandi
Risyandi / arrayMultidimension.js
Created September 13, 2021 05:30
convert array object, to array multidimension
let dataProps = [{
"id": 1,
"mat_doc": "5000001724",
"mat_doc_year": 2020,
"mat_doc_item": 1,
"ref_doc_num": "SJ1234567892",
"header_text": null,
"posting_date": "2020-08-09T17:00:00.000Z",
"doc_date": "2020-08-09T17:00:00.000Z",
"move_type": "101",
@Risyandi
Risyandi / createPdf.js
Created September 13, 2021 05:26
create pdf using pdfMake library
// library pdfMake
import pdfMake from "pdfmake/build/pdfmake";
import pdfFonts from "pdfmake/build/vfs_fonts";
pdfMake.vfs = pdfFonts.pdfMake.vfs;
// document definition
let docDefinition = {
content: [{
text: 'Report Goods Receive',
style: 'header'
@Risyandi
Risyandi / clearReactSelect.js
Created July 28, 2021 03:46
line code for clear react select
import React, { useRef } from "react";
import Select from "react-select";
export default function App() {
const selectInputRef = useRef();
const onClear = () => {
selectInputRef.current.select.clearValue();
};