Categories
JavaScript Answers

How to Round Time to the Nearest Quarter-Hour in JavaScript?

To round time to the nearest quarter-hour in JavaScript, we can use existing JavaScript date methods.

For instance, we can write:

const roundTimeQuarterHour = (time) => {
  const timeToReturn = new Date(time);
  timeToReturn.setMilliseconds(Math.round(timeToReturn.getMilliseconds() / 1000) * 1000);
  timeToReturn.setSeconds(Math.round(timeToReturn.getSeconds() / 60) * 60);
  timeToReturn.setMinutes(Math.round(timeToReturn.getMinutes() / 15) * 15);
  return timeToReturn;
}
const dt = new Date(2021, 1, 1, 1, 13, 1, 1)
console.log(roundTimeQuarterHour(dt))

to create the roundTimeQuarterHour function that takes the time to round.

In the function, we convert that to a date object with the Date constructor top create the timeToReturn date object.

Then we call setMilliseconds to set timeToReturn the nearest 1000 milliseconds.

Next, we call setSeconds to round timeToReturn to the nearest 60 seconds.

Then, we call setNMinutes to round timeToReturn to the nearest 15 minutes by getting the number of minutes from the time with getMinutes , dividing that by 15 and multiplying that by 15 again.

Therefore, the console log should log:

Mon Feb 01 2021 01:15:00 GMT-0800 (Pacific Standard Time)
Categories
JavaScript Answers

How to Do Gaussian/Banker’s Rounding in JavaScript?

To do Gaussian/Banker’s rounding in JavaScript, we can create our own function to do the rounding.

For instance, we write:

const isEven = (n) => {
  return (0 === (n % 2));
};

const bankersRound = (x) => {
  const r = Math.round(x);
  return (((((x > 0) ? x : (-x)) % 1) === 0.5) ? ((isEven(r)) ? r : (r - 1)) : r);
};

console.log(bankersRound(1))
console.log(bankersRound(1.5))
console.log(bankersRound(2.5))

We first create the isEven function to return whether the number n is an even number.

Then we create the bankersRound function to round the number.

In the function, we first round the number x with Math.round .

Then we check if x is bigger than 0.

If it is, then we return x .

Otherwise, we return -x .

And we check if that number divided by 1 has 0.5 as the remainder.

If it does, we check if the number if even with isEven .

If it is, we return r .

Otherwise, we return r — 1 .

If x ends with .5, then we also return r .

Therefore, we see the first console log logs 1 and the other 2 logs 2.

Categories
JavaScript Answers

How to Disable Clicking Inside a Div with CSS or JavaScript?

To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none .

Also, we can add a click event listener to the div and then call event.preventDefault inside.

For instance, if we have the following div:

<div id='foo'>  
  foo  
</div>

Then we can disable clicks inside it with CSS by writing:

#foo {  
  pointer-events: none;  
}

To disable clicks with JavaScript, we can write:

const foo = document.querySelector('#foo')  
foo.addEventListener('click', (event) => {  
  event.preventDefault();  
});

to select the div by its ID with document.querySelector .

Then we call addEventListener on it with 'click' and a click event handler that calls event.preventDefault to stop the default action when the div is clicked.

Categories
JavaScript Answers jQuery

How to Use in jQuery hasClass Method to Get a Specific Element Without a Given Class?

To use in jQuery hasClass Method to get a specific element without a given class, we can use the hasClass method with the ! operator to find the element without the given class.

For instance, if we have the following HTML:

<div id='foo'>  
  foo  
</div>

Then we can use:

if (!$('#foo').hasClass('bar')) {  
  //...  
}

to check the div with ID foo has the class bar .

hasClass returns true if the element has the even class, so we can negate that with the ! operator.

If it doesn’t then we do something in the if statement body.

Categories
JavaScript Answers

How to Extract Part of an Array with JavaScript?

To extract part of an array with JavaScript, we can use the JavaScript array slice method with the start and end index of the array to extract.

For instance, we can write:

const oldArray = [1, 2, 3, 4, 5]
const newArray = oldArray.slice(1, 3);
console.log(newArray)

We call slice on oldArray with the start and end indexes respectively.

The end index is excluded from the returned array.

So the items at indexes 1 and 2 will be included in the returned array.

Therefore, newArray is [2, 3] .