Categories
JavaScript Answers

How to replace HTML page with contents retrieved via Ajax with JavaScript?

Sometimes, we want to replace HTML page with contents retrieved via Ajax with JavaScript.

In this article, we’ll look at how to replace HTML page with contents retrieved via Ajax with JavaScript.

How to replace HTML page with contents retrieved via Ajax with JavaScript?

To replace HTML page with contents retrieved via Ajax with JavaScript, we set the Ajax response to document.body.innerHTML.

For instance, we write

<html>
  <body>
    blablabla
    <script type="text/javascript">
      document.body.innerHTML = "hi!";
    </script>
  </body>
</html>

to set 'hi!' as the value of document.body.innerHTML to update it.

Conclusion

To replace HTML page with contents retrieved via Ajax with JavaScript, we set the Ajax response to document.body.innerHTML.

Categories
JavaScript Answers

How to create a plain count up timer in JavaScript?

Sometimes, we want to create a plain count up timer in JavaScript.

In this article, we’ll look at how to create a plain count up timer in JavaScript.

How to create a plain count up timer in JavaScript?

To create a plain count up timer in JavaScript, we use the setInterval function.

For instance, we write

<label id="minutes">00</label>:<label id="seconds">00</label>

to add the label elements for the minutes and seconds displays.

Then we write

const minutesLabel = document.getElementById("minutes");
const secondsLabel = document.getElementById("seconds");
let totalSeconds = 0;

const setTime = () => {
  ++totalSeconds;
  secondsLabel.innerHTML = (totalSeconds % 60).toString().padStart(2, "0");
  minutesLabel.innerHTML = parseInt(totalSeconds / 60)
    .toString()
    .padStart(2, "0");
};

setInterval(setTime, 1000);

to select the elements with getElementById.

Then we define the setTime function that updates the elements by getting the seconds with totalSeconds % 60.

We prepend a 0 before it when its length is less than 2.

Likewise, we get the minutes with totalSeconds / 60 and call padStart to prepend a 0 before it when its length is less than 2.

We set innerHTML of the elements to display the numbers.

Conclusion

To create a plain count up timer in JavaScript, we use the setInterval function.

Categories
JavaScript Answers

How to mock or replace getter function of object with Jest and JavaScript?

Sometimes, we want to mock or replace getter function of object with Jest and JavaScript.

In this article, we’ll look at how to mock or replace getter function of object with Jest and JavaScript.

How to mock or replace getter function of object with Jest and JavaScript?

To mock or replace getter function of object with Jest and JavaScript, we can call the spyOn method.

For instance, we write

class MyClass {
  get something() {
    return "foo";
  }
}

jest.spyOn(MyClass, "something", "get").mockReturnValue("bar");
const something = new MyClass().something;


to call jest.spyOn with MyClass, "something", and 'get' to mock the something getter in MyClass.

We call mockReturnValue to return 'bar' as the value of something.

Then we check if something is 'bar' with

expect(something).toEqual("bar");

Conclusion

To mock or replace getter function of object with Jest and JavaScript, we can call the spyOn method.

Categories
React Answers

How to detect when user scrolls to bottom of div with React?

Sometimes, we want to detect when user scrolls to bottom of div with React.

In this article, we’ll look at how to detect when user scrolls to bottom of div with React.

How to detect when user scrolls to bottom of div with React?

To detect when user scrolls to bottom of div with React, we check if the sum of the scrollTop and clientHeight of the scroll container is the same as its scrollHeight.

For instance, we write

import React, { useRef, useEffect } from "react";

const MyListComponent = () => {
  const listInnerRef = useRef();

  const onScroll = () => {
    if (listInnerRef.current) {
      const { scrollTop, scrollHeight, clientHeight } = listInnerRef.current;
      if (scrollTop + clientHeight === scrollHeight) {
        // ...
        console.log("Reached bottom");
      }
    }
  };

  return (
    <div className="list">
      <div className="list-inner" onScroll={onScroll} ref={listInnerRef}>
        {/* List items */}
      </div>
    </div>
  );
};

to call onScroll when we scroll by setting the onScroll prop of the div to onScroll.

Then we assign a listInnerRef to the div.

In onScroll, we check if the sum of the scrollTop and clientHeight of the scroll container is the same as its scrollHeight.

If it is, then the bottom of the element is reached.

Conclusion

To detect when user scrolls to bottom of div with React, we check if the sum of the scrollTop and clientHeight of the scroll container is the same as its scrollHeight.

Categories
JavaScript Answers

How to check if a number is prime in JavaScript?

Sometimes, we want to check if a number is prime in JavaScript.

In this article, we’ll look at how to check if a number is prime in JavaScript.

How to check if a number is prime in JavaScript?

To check if a number is prime in JavaScript, we can use a loop.

For instance, we write

const isPrime = (num) => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
    if (num % i === 0) {
      return false;
    }
  }
  return num > 1;
};

to define the isPrime function.

In it, we loop from 2 to the square root of num.

In the loop body, we check if num can be evenly divisible by i with num % i === 0.

And if it’s true, we return false since it’s not prime.

If it’s evenly divisible by nothing, we return true.

Conclusion

To check if a number is prime in JavaScript, we can use a loop.