Categories
JavaScript Answers

How to repeat code every 4 seconds with JavaScript?

Sometimes, we want to repeat code every 4 seconds with JavaScript.

In this article, we’ll look at how to repeat code every 4 seconds with JavaScript.

How to repeat code every 4 seconds with JavaScript?

To repeat code every 4 seconds with JavaScript, we can call setInterval.

For instance, we write:

const fn = () => {
  console.log('hello')
}
setInterval(fn, 4000)

to call setInterval with fn and 4000 to run fn every 4 seconds.

As a result, 'hello' is logged every 4 seconds.

Conclusion

To repeat code every 4 seconds with JavaScript, we can call setInterval.

Categories
JavaScript Answers

How to validate a credit card number with JavaScript?

Sometimes, we want to validate a credit card number with JavaScript.

In this article, we’ll look at how to validate a credit card number with JavaScript.

How to validate a credit card number with JavaScript?

To validate a credit card number with JavaScript, we can use the Lyhn algorithm.

For instance, we write:

const checkLuhn = (cardNo) => {
  let s = 0;
  let doubleDigit = false;
  for (const d of [...cardNo].reverse()) {
    let digit = +d;
    if (doubleDigit) {
      digit *= 2;
      if (digit > 9)
        digit -= 9;
    }
    s += digit;
    doubleDigit = !doubleDigit;
  }
  return s % 10 === 0;
}

console.log(checkLuhn('4242424242424242'))
console.log(checkLuhn('8242424242424242'))

to define the checkLuhn function that uses the Luhn algorithm to check if cardNo is a credit card number.

The steps in the algorithm are:

  1. If the number already contains the check digit, drop that digit to form the "payload." The check digit is most often the last digit.
  2. With the payload, start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit).
  3. Sum the digits of the resulting value in each position.
  4. Sum the resulting values from all position s.

Steps 2 to 4 are done with

for (const d of [...cardNo].reverse()) {
  let digit = +d;
  if (doubleDigit) {
    digit *= 2;
    if (digit > 9)
      digit -= 9;
  }
  s += digit;
  doubleDigit = !doubleDigit;
}
  1. The check digit is calculated by s % 10 === 0.

Therefore, the first console log logs true and the 2nd logs false.

Conclusion

To validate a credit card number with JavaScript, we can use the Lyhn algorithm.

Categories
JavaScript Answers

How to fix the “Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null” error with JavaScript?

Sometimes, we want to fix the "Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null" error with JavaScript.

In this article, we’ll look at how to fix the "Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null" error with JavaScript.

How to fix the "Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null" error with JavaScript?

To fix the "Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null" error with JavaScript, we should move script tags into the body element to make sure it’s run when the body element is loaded.

For instance, we write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Page</title>
  </head>
  <body>
    <div></div>
    <div></div>
    <script>
      console.log(document.body.getElementsByTagName("div"));
    </script>
  </body>
</html>

to call document.body.getElementsByTagName with 'div' to select the divs after they’re loaded.

We make sure they’re loaded before running the script by putting the script tag below the divs within body.

Conclusion

To fix the "Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null" error with JavaScript, we should move script tags into the body element to make sure it’s run when the body element is loaded.

Categories
JavaScript Answers

How to wait for a click event to complete with JavaScript?

Sometimes, we want to wait for a click event to complete with JavaScript.

In this article, we’ll look at how to wait for a click event to complete with JavaScript.

How to wait for a click event to complete with JavaScript?

To wait for a click event to complete with JavaScript, we can add a click listener function.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div');
div.onclick = () => {
  console.log('clicked')
}

to select the div with querySelector.

Next, we set div.onclick to a function that logs 'clicked'.

Now when the click event is emitted, we see 'clicked' logged.

Conclusion

To wait for a click event to complete with JavaScript, we can add a click listener function.

Categories
JavaScript Answers

How to read individual transform style values in JavaScript?

Sometimes, we want to read individual transform style values in JavaScript.

In this article, we’ll look at how to read individual transform style values in JavaScript.

How to read individual transform style values in JavaScript?

To read individual transform style values in JavaScript, we can use some string and array methods.

For instance, we write:

<div style="transform: translate(10px, 20px)">
  hello world
</div>

to add a div.

Then we write:

const element = document.querySelector('div');
const styles = window.getComputedStyle(element, null);
const matrixPattern = /^\w*\((((\d+)|(\d*\.\d+)),\s*)*((\d+)|(\d*\.\d+))\)/i;
const vals = [...styles.transform.match(matrixPattern)].slice(1).filter(Boolean).map(v => +v.replace(/\D+/, ''))
console.log(vals)

to select the div with querySelector.

Then we get the styles with getComputedStyle.

Next, we call styles.transform.match with matrixPattern to get all the numbers from the CSS style value.

Then we spread the values into an array.

Next, we call slice to remove the match with all the number values in one string.

Then we call filter to remove the falsy values.

Finally, we call map with a callback to remove all non-digit characters and use + to convert the string to number.

As a result, vals is [10, 10, 10, 20, 20].

Conclusion

To read individual transform style values in JavaScript, we can use some string and array methods.