Categories
JavaScript Answers

How to get a date in YYYY-MM-DD format with JavaScript?

To get a date in YYYY-MM-DD format, we can use the date toISOString method.

For instance, we write:

const d = new Date(2022, 2, 2)
const [date] = d.toISOString().split('T')
console.log(date)

to create a new date with the Date constructor.

Then we call toISOString on the date instance to return a date time string.

Next, we split the string with split called with 'T' to split the date time string into the date and time parts.

Finally, we get the date part with destructuring.

Therefore, date is "2022-03-02".

Conclusion

To get a date in YYYY-MM-DD format, we can use the date toISOString method.

Categories
JavaScript Answers

How to count the number of properties in a JavaScript object?

Sometimes, we want to count the number of properties in a JavaScript object.

In this article, we’ll look at how to count the number of properties in a JavaScript object.

How to count the number of properties in a JavaScript object?

To count the number of properties in a JavaScript object, we can use the Object.entries method.

For instance, we write:

const member = {
  "mother": {
    "name": "Mary",
    "age": "38"
  },
  "father": {
    "name": "Bill",
    "age": "40"
  },
  "brother": {
    "name": "Alex",
    "age": "10"
  }
}

const {
  length
} = Object.entries(member)
console.log(length)

to call Object.entries with member to return an array of key-value pair arrays obtained from member.

Then we use the length property of the returned array to get the number of properties in member.

Therefore, length is 3.

Conclusion

To count the number of properties in a JavaScript object, we can use the Object.entries method.

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.