Categories
JavaScript Answers

How to find prime numbers between 0 – 100 with JavaScript?

Sometimes, we want to find prime numbers between 0 – 100 with JavaScript.

In this article, we’ll look at how to find prime numbers between 0 – 100 with JavaScript.

How to find prime numbers between 0 – 100 with JavaScript?

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.

For instance, we write

const isPrime = (n) => {
  if (n < 2) {
    return false;
  }

  const q = Math.floor(Math.sqrt(n));

  for (let i = 2; i <= q; i++) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
};

to define the isPrime function.

In it, we return false if n is less than 2.

Otherwise, we loop from 2 to the floor of the square root of n.

And we check if each value i is evenly divides n.

If that’s the case, we return false.

Otherwise, we return true.

Conclusion

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.

Categories
JavaScript Answers

How to toggle class using pure JavaScript in HTML?

Sometimes, we want to toggle class using pure JavaScript in HTML.

In this article, we’ll look at how to toggle class using pure JavaScript in HTML.

How to toggle class using pure JavaScript in HTML?

To toggle class using pure JavaScript in HTML, we use the classList property.

For instance, we write

const container = document.querySelector("#container");

container.addEventListener("mouseenter", (e) => {
  e.target.classList.remove("first");
  e.target.classList.add("second");
});

container.addEventListener("mouseleave", (e) => {
  e.target.classList.add("first");
  e.target.classList.remove("second");
});

to select the element with querySelector.

Then we call addEventListener to listen to the mouseenter and `mouseleave events.

In the listeners, we call e.target.classList.remove to remove the class from the current element.

And we use e.target.classList.add to add a class to the current element.

Conclusion

To toggle class using pure JavaScript in HTML, we use the classList property.

Categories
JavaScript Answers

How to remove object from scene with Three.js and JavaScript?

Sometimes, we want to remove object from scene with Three.js and JavaScript.

In this article, we’ll look at how to remove object from scene with Three.js and JavaScript.

How to remove object from scene with Three.js and JavaScript?

To remove object from scene with Three.js and JavaScript, we use the remove method.

For instance, we write

const removeEntity = (object) => {
  const selectedObject = scene.getObjectByName(object.name);
  scene.remove(selectedObject);
  animate();
};

to define the removeEntity function.

In it, we call scene.getObjectByName to get the object by the name of the object.

Then we call scene.remove with selectedObject to remove it.

Finally we call animate to animate the changes.

Conclusion

To remove object from scene with Three.js and JavaScript, we use the remove method.

Categories
JavaScript Answers

How to create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript?

Sometimes, we want to create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript.

In this article, we’ll look at how to create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript.

How to create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript?

To create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript, we call history.back when we click on the link.

For instance, we write

<a href="javascript:history.back();">Go Back</a>

to call history.back when we click on the Go Back link to go back to the previous page.

Conclusion

To create a ‘Go Back’ link that takes the user to a link if there’s no history for the tab or window with JavaScript, we call history.back when we click on the link.

Categories
JavaScript Answers

How to make axios synchronous with JavaScript?

Sometimes, we want to make axios synchronous with JavaScript.

In this article, we’ll look at how to make axios synchronous with JavaScript.

How to make axios synchronous with JavaScript?

To make axios synchronous with JavaScript, we use the await keyword.

For instance, we write

const response = await axios.get("/api/persons/unique/alias");

to call axios.get to make a get request and return a promise with the response.

Then we use await to wait for the response and get it.

Conclusion

To make axios synchronous with JavaScript, we use the await keyword.