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
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 / 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
@Risyandi
Risyandi / dateDiff.js
Created February 16, 2023 04:30
how to check date
function dateDiff(startDate, endDate) {
console.log(new Date(startDate), "startDate"); // since user register
console.log(new Date(endDate), "endDate"); // date of voucher expired
const diffInMs = new Date(endDate) - new Date(startDate);
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
console.log(diffInDays, "diffInDays");
if (diffInDays === 5) {
console.log("voucher disappear");
// Set initial countdown time to 60 seconds
var countdown = 60;
// Update the timer display every second
setInterval(function() {
countdown--;
var seconds = countdown % 60;
var minutes = Math.floor(countdown / 60);
// Add leading zero to seconds if less than 10
@Risyandi
Risyandi / iframeFullPage.vue
Last active March 16, 2023 03:39
how to create page with contain iframe full page responsive and access others website in nuxtjs
<template>
<div class="iframe-container">
<iframe
class="iframe"
:src="iframeUrl"
allowfullscreen
scrolling="yes"
ref="iframe"
@load="handleIframeLoad"
></iframe>
@Risyandi
Risyandi / pauseExecutionGoroutine.go
Created March 18, 2023 14:52
how to pause the execution of current go routine
// Go program to illustrate how
// to put a goroutine to sleep
package main
import (
"fmt"
"time"
)
// Here, the value of Sleep function is zero
@Risyandi
Risyandi / generateId.js
Last active May 5, 2023 08:50
generateID using javascript
function generateID() {
const date = new Date();
const day = ("0" + date.getDate()).slice(-2);
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const year = date.getFullYear().toString().substr(-2);
const random = Math.random().toString(36).substring(2, 6).toUpperCase();
return `INV-DOL-${day}${month}${year}-${random}`;
}
const newID = generateID();
@Risyandi
Risyandi / convertTime24to12hour.go
Created May 19, 2023 08:18
convert AM/PM 12 hour to 24 hour to time using golang
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
)
@Risyandi
Risyandi / stringToSnakeCase.js
Created June 6, 2023 08:49
convert text string to tsnake case, and remove special character by condition.
function convertToSnakeCase(text) {
let snakeCaseText = "";
const specialChars = `!"#$%&'()*+,-./:;<=>?@[\\]^\`{|}~`;
for (let index = 0; index < text.length; index++) {
const char = text[index];
if (char === "_" || char === " ") {
snakeCaseText += "_"; // Preserve underscores and change spaces to underscores
} else if (!specialChars.includes(char)) {