Categories
JavaScript Answers

How to change onClick handler dynamically with JavaScript?

Sometimes, we want to change onClick handler dynamically with JavaScript.

In this article, we’ll look at how to change onClick handler dynamically with JavaScript.

How to change onClick handler dynamically with JavaScript?

To change onClick handler dynamically with JavaScript, we set the onclick property of an element.

For instance, we write

document.getElementById("foo").onclick = () => {
  alert("foo");
};

to select the element we want to attach the click listener to with getElementById.

Then we set its onclick property to a function that shows an alert when we click on the element.

Conclusion

To change onClick handler dynamically with JavaScript, we set the onclick property of an element.

Categories
JavaScript Answers

How to validate phone number with Yup and JavaScript?

Sometimes, we want to validate phone number with Yup and JavaScript.

In this article, we’ll look at how to validate phone number with Yup and JavaScript.

How to validate phone number with Yup and JavaScript?

To validate phone number with Yup and JavaScript, we use the matches method

For instance, we write

const phoneRegExp =
  /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/;

const schema = yup.object().shape({
  phone: yup
    .string()
    .matches(phoneRegExp, {
      message: "Invalid phone number",
      excludeEmptyString: false,
    })
    .required(),
});

to call yup.string to validate that phone is a string.

Then we call matches to check if phone matches the phone regex value.

We check all the segments of any phone number in the world with the regex.

Conclusion

To validate phone number with Yup and JavaScript, we use the matches method

Categories
JavaScript Answers

How to use expression inside switch case statement with JavaScript?

Sometimes, we want to use expression inside switch case statement with JavaScript.

In this article, we’ll look at how to use expression inside switch case statement with JavaScript.

How to use expression inside switch case statement with JavaScript?

To use expression inside switch case statement with JavaScript, we use switch with true.

For instance, we write

switch (true) {
  case amount >= 7500 && amount < 10000:
    // ...
    break;
  case amount >= 10000 && amount < 15000:
    // ...
    break;
  // ...
}

to use switch with true.

Then we can use any boolean expression with case to run code according to the amount value.

Conclusion

To use expression inside switch case statement with JavaScript, we use switch with true.

Categories
JavaScript Answers

How to splice an array in half, no matter the size with JavaScript?

Sometimes, we want to splice an array in half, no matter the size with JavaScript.

In this article, we’ll look at how to splice an array in half, no matter the size with JavaScript.

How to splice an array in half, no matter the size with JavaScript?

To splice an array in half, no matter the size with JavaScript, we get the index of the middle element in the array and use slice with that.

For instance, we write

const halfLength = Math.ceil(arrayName.length / 2);
const leftSide = arrayName.slice(0, halfLength);

to get the halfLength index.

We get the ceiling of the middle array length divide by 2 and use that with slice to get the first half the array.

The 2nd half we get by writing

const rightSide = arrayName.slice(halfLength);

Conclusion

To splice an array in half, no matter the size with JavaScript, we get the index of the middle element in the array and use slice with that.

Categories
JavaScript Answers

How to generate all combinations of elements in a single array in pairs with JavaScript?

Sometimes, we want to generate all combinations of elements in a single array in pairs with JavaScript.

In this article, we’ll look at how to generate all combinations of elements in a single array in pairs with JavaScript.

How to generate all combinations of elements in a single array in pairs with JavaScript?

To generate all combinations of elements in a single array in pairs with JavaScript, we use the array map and flatMap methods.

For instance, we write

const array = ["apple", "banana", "lemon", "mango"];

const result = array.flatMap((v, i) =>
  array.slice(i + 1).map((w) => v + " " + w)
);

console.log(result);

to call slice and map to create all the strings with 2 of the items in array in each string separated by a space.

Then we call flatMap to combine the nested arrays into one flat array.

Conclusion

To generate all combinations of elements in a single array in pairs with JavaScript, we use the array map and flatMap methods.