Generate Ranged Random Integer in Javascript

Here I will show you a simple way to generate a ranged random integer number in javascript. Since javascript doesn’t have built in function to generate ranged integer number, we will create one. function randInt(left, right) { return Math.floor(Math.random() * (right - left + 1)) + left; } And that’s it, this is just me passing by and dropping some code snippets. See you later. Stay safe!

October 26, 2024 · 1 min · Deni Andrian Prayoga

Replicating Right Click Behavior using Vanilla Javascript

The idea is simple. First, disable the default event handler for right click in the browser. Second, create a custom event handler for right click event. Implementing the first idea, we can do something like the following. window.addEventListener("contextmenu", (event) => { event.preventDefault(); }); from here we can easily add any custom code for event handler to our liking, here I will just log “Right Click detected!” into the console. window.addEventListener("contextmenu", (event) => { event.preventDefault(); console.log("Right Click detected!"); }); You can create additional html elements and put it into the window to act as the replacement for the default context menu. ...

October 24, 2024 · 1 min · Deni Andrian Prayoga

Javascript Promise Simplified

Promise has three different states: pending, resolve, and reject. When you first create a promise, it will be in pending state. This promise can be either “fulfilled” thus it will be resolved or it can be “rejected” thus it will be rejected. In other word, if promise resulted in success it resolve, else it reject. To create a promise in javascript is really simple. const myPromise = new Promise(function (resolve, reject) { // do something }); What you do inside the promise will decide whether it will resolve or reject. ...

October 21, 2024 · 2 min · Deni Andrian Prayoga