Categories

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

Sometimes, we want to do Gaussian/Banker’s rounding in JavaScript.

In this article, we’ll look at how to do Gaussian/Banker’s rounding in JavaScript.

### 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.

### Conclusion

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

Categories

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

Sometimes, we want to disable clicking inside a div with CSS or JavaScript

In this article, we’ll look at how to disable clicking inside a div with CSS or JavaScript.

### 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.

### Conclusion

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.

Categories

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

Sometimes, we want to use in jQuery hasClass Method to get a specific element without a given class.

In this article, we’ll look at how to use in jQuery hasClass Method to get a specific element without a given class.

### 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.

### Conclusion

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.

Categories

## How to Extract Part of an Array with JavaScript?

Sometimes, we want to extract part of an array with JavaScript.

In this article, we’ll look at how to extract part of an array with JavaScript.

### 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]` .

### Conclusion

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.

Categories

## How to Iterate Over JavaScript Set Elements?

Sometimes, we want to iterate over JavaScript set elements.

In this article, we’ll look at how to iterate over JavaScript set elements.

### Iterate Over JavaScript Set Elements

To iterate over JavaScript set elements, we can use the for-of loop or the JavaScript set’s `forEach` method.

For instance, to iterate over a set with a for-of loop, we can write:

``````const set = new Set([1, 2, 3]);

for (const el of set) {
console.log(el)
}
``````

We create the `set` set with the `Set` constructor with an array passed into it.

Then we use the for-of loop below that to loop through the items of `set` .

And so we should see 1, 2, and 3 logged.

To use the `forEach` method, we can write:

``````const set = new Set([1, 2, 3]);
set.forEach(el => console.log(el))
``````

We call `forEach` with a callback with the `el` parameter logged in it.

And so we get the same result as before.

### Conclusion

To iterate over JavaScript set elements, we can use the for-of loop or the JavaScript set’s `forEach` method.