Categories
JavaScript Answers

How to get a random number between 0.0200 and 0.120 with JavaScript?

Sometimes, we want to get a random number between 0.0200 and 0.120 with JavaScript.

In this article, we’ll look at how to get a random number between 0.0200 and 0.120 with JavaScript.

How to get a random number between 0.0200 and 0.120 with JavaScript?

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.

For instance, we write

const n = (Math.random() * (0.12 - 0.02) + 0.02).toFixed(4);

to use Math.random() * (0.12 - 0.02) + 0.02 to return a number between 0.02 and 0.12.

Then we call toFixed with 4 to round the returned number to 4 decimal places.

Conclusion

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.

Categories
JavaScript Answers

How to filter on array of RXJS Observable with JavaScript?

Sometimes, we want to filter on array of RXJS Observable with JavaScript.

In this article, we’ll look at how to filter on array of RXJS Observable with JavaScript.

How to filter on array of RXJS Observable with JavaScript?

To filter on array of RXJS Observable with JavaScript, we use the filter function.

For instance, we write

import { filter } from "rxjs/operators";

const filteredEpicsObs = getEpics()
  .pipe(filter((epic) => epic.id === id))
  .subscribe((x) => {
    //...
  });

to call pipe on the observable returned by getEpics.

We call pipe with the filter returned by filter, which is called with a callback to match the values with the given id.

Then we get the filtered results from the subscribe callback.

Conclusion

To filter on array of RXJS Observable with JavaScript, we use the filter function.

Categories
JavaScript Answers

How to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript?

Sometimes, we want to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript.

In this article, we’ll look at how to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript.

How to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript?

To remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript, we call replace with a regex.

For instance, we write

const newString = str.replace(/[^A-Z0-9]/gi, "_");

to call str.replace with a regex that matches anything that aren’t alphanumeric characters.

The g flag makes replace replace all matches.

And i makes replace find matches in a case-insensitive manner.

We replace the matches with underscores.

Conclusion

To remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript, we call replace with a regex.

Categories
JavaScript Answers

How to round a number in JavaScript?

Sometimes, we want to round a number in JavaScript.

In this article, we’ll look at how to round a number in JavaScript.

How to round a number in JavaScript?

To round a number in JavaScript, we use the Math.round method.

For instance, we write

const rounded = Math.round(332.24);

to call Math,round with the number we want to round.

It returns the rounded number as an integer.

Conclusion

To round a number in JavaScript, we use the Math.round method.

Categories
JavaScript Answers

How to click a button programmatically with JavaScript?

Sometimes, we want to click a button programmatically with JavaScript.

In this article, we’ll look at how to click a button programmatically with JavaScript.

How to click a button programmatically with JavaScript?

To click a button programmatically with JavaScript, we can call the click method.

For instance, we write

const userImage = document.getElementById("imageOtherUser");
const hangoutButton = document.getElementById("hangoutButtonId");
userImage.onclick = () => {
  hangoutButton.click();
};

to select the buttons with getElementById.

Then we add a click event handler to the userImage button by setting its onclick property to a function that calls hangoutButton.click.

This will click on the hangoutButton button programmatically.

Conclusion

To click a button programmatically with JavaScript, we can call the click method.