JavaScript Problems — Adding Minutes, Find Min and Max, and More
By: John Au-Yeung
Tags: Programming, Web Development, JavaScript, Software Development, Technology

Photo by Rachel Hisko on Unsplash

Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Add Minutes to a JavaScript Date Object

There are a few ways to add a given number of minutes to a JavaScript.

For instance, we can add 50 minutes to a JavaScript Date object by writing:

const newDateObj = moment(oldDateObj).add(50, 'm').toDate();

We used moment.js to convert the oldDateObj date object into a moment object.

Then we add 50 minutes to it with the add method.

50 is the minutes, 'm' stands for minutes.

Then we convert it back to a Date instance with the toDate method.

To do the same thing with plain JavaScript, we can add the minutes by writing:

const newDateObj = new Date(oldDateObj.getTime() + 50 * 1000 * 60);

getTime converts the Date instance into a UNIX timestamp, which is the number of milliseconds since Jan 1, 1970 12 AM UTC.

Then we add 50 * 1000 * 60 , which is the number of milliseconds in 50 minutes.

We convert to seconds by multiplying by 60 and we convert to milliseconds by multiplying by 1000.

However, we have to be careful with daylight saving time.

The plain JavaScript way doesn’t take into account daylight saving time.

Get a Hash Value in a URL

We can set the window.location.hash to get the value after the hash sign.

Then we can use substring(1) to get the value without the hash.

Therefore, we can write:

window.location.hash.substring(1)

to get the value.

Remove All Classes using jQuery and JavaScript

We can call removeClass that’s included with jQuery to remove all classes.

For instance, we can write:

$("#item").removeClass();

to remove all the classes from the element with ID item .

Alternatively, we can use removeAttr :

$("#item").removeAttr('class');

We can use attr :

$("#item").attr('class', '');

to set the class value blank.

Likewise, we can write:

$('#item')[0].className = '';

If we want to use plain JavaScript, we can write:

document.getElementById('item').className = '';

We just set the className property to an empty string.

Format Number to always Show in 2 Decimal Places

We can round to 2 decimal places with:

Number(1.333).toFixed(2);

to get ‘1.33’ .

toFixed formats the number to a given number of decimal places.

It returns the string representation of it.

prototype and this

There is a difference between using the proottype for instance methods and attaching them to this inside the constructor function.

So, this:

const A = function () {

this.x = function () {

// ...

};

};

is different from:

const A = function () { };

A.prototype.x = function () {

// ...

};

The first way creates separate copies of instance methods within the constructor function whenever we instantiate an instance of it.

The instance methods added to the prototype is reused among all instances in memory.

However, they act the same in that they work on their own instances only.

Therefore, if we write instance methods, we should attach them to prototype since we only have one copy of it in memory, so less memory is used.

Find the Min and Max Values in a JavaScript Array

There are a few ways to find the min and max values in a JavaScript array.

For instance, we can write:

const min = Math.min.apply(null, arr),

const max = Math.max.apply(null, arr);

arr is the array.

apply calls the Math.min or Math.max with the entries in arr as the arguments.

Math.min returns the minimum value of an array and Math.max returns the max value.

They take a comma-separated list of arguments.

A more modern alternative is to use the spread operator.

For instance, we can write:

const min = Math.min(...arr),

const max = Math.max(...arr);

We spread the array entries into arguments with the spread operator.

It’s shorter and gets the same results.

Photo by Sean O. on Unsplash

Conclusion

We can find the max and min values from an array in various ways.

They all use the Math.min and Math.max methods.

Attaching to prototype is better for instance methods than attaching directly to this .

There are various ways to remove classes from a DOM element.