Categories
JavaScript Answers

How to check if a string ends with a substring with JavaScript?

Sometimes, we want to check if a string ends with a substring with JavaScript.

In this article, we’ll look at how to check if a string ends with a substring with JavaScript.

How to check if a string ends with a substring with JavaScript?

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.

For instance, we write

const endsWithDog = "this is dog".endsWith("dog");

to call "this is dog".endsWith with 'dog' to check if "this is dog" ends with 'dog'.

It should return true since 'dog' is at the end of "this is dog".

Conclusion

To check if a string ends with a substring with JavaScript, we can use the string endsWith method.

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