Categories
JavaScript Answers

How to create a simple, non-secure hash function for JavaScript?

Sometimes, we want to create a simple, non-secure hash function for JavaScript.

In this article, we’ll look at how to create a simple, non-secure hash function for JavaScript.

How to create a simple, non-secure hash function for JavaScript?

To create a simple, non-secure hash function for JavaScript, we can create our own function.

For instance, we write

const hashCode = (str) => {
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    const char = str.charCodeAt(i);
    hash = (hash << 5) - hash + char;
    hash = hash & hash;
  }
  return hash;
};

to create the hashCode function that takes the str string and creates a hash.

In it, we loop through the characters in str.

And we update the hash by doing a left shift by 5 bits.

Then we subtract the hash and add the character code of the character at i to it.

And the we convert hash to a 32 bit integer with hash & hash.

Conclusion

To create a simple, non-secure hash function for JavaScript, we can create our own function.

Categories
JavaScript Answers

How to use typeof with RegExp in JavaScript?

Sometimes, we want to use typeof with RegExp in JavaScript.

In this article, we’ll look at how to use typeof with RegExp in JavaScript.

How to use typeof with RegExp in JavaScript?

To use typeof with RegExp in JavaScript, we replace typeof with instanceof.

For instance, we write

const t = /^foo(bar)?$/i;
console.log(t instanceof RegExp);

to check if t is an instance of RegExp with instanceof.

t instanceof RegExp should be true since t is a regex object.

Conclusion

To use typeof with RegExp in JavaScript, we replace typeof with instanceof.

Categories
JavaScript Answers

How to get index of element as child relative to parent with JavaScript?

Sometimes, we want to get index of element as child relative to parent with JavaScript.

In this article, we’ll look at how to get index of element as child relative to parent with JavaScript.

How to get index of element as child relative to parent with JavaScript?

To get index of element as child relative to parent with JavaScript, we can spread the node list returned by querySelectorAll into an array.

For instance, we write

const lis = [...document.querySelectorAll("#wizard > li")];

lis.forEach((li) => {
  li.addEventListener("click", () => {
    const index = lis.indexOf(li);
    console.log(index);
  });
});

to call querySelectorAll to select the li’s.

Then we spread the returned node list to an array with ...

Next, we call forEach on the lis array with a callback and call addEventListener on each li to add click listeners to each li.

Conclusion

To get index of element as child relative to parent with JavaScript, we can spread the node list returned by querySelectorAll into an array.

Categories
JavaScript Answers

How to get the body’s content of an iframe in JavaScript?

Sometimes, we want to get the body’s content of an iframe in JavaScript.

In this article, we’ll look at how to get the body’s content of an iframe in JavaScript.

How to get the body’s content of an iframe in JavaScript?

To get the body’s content of an iframe in JavaScript, we can use the iframe’s contentDocument property.

For instance, we write

const iframe = document.querySelector("#id_description_iframe");
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const iframeContent = iframeDocument.querySelectorAll("#frameBody");

to select the iframe with querySelector.

Then we get the DOM object of the iframe’s content with

const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

And then we use querySelectorAll on the iframeDocument to select the elements we want from the iframe’s contents.

Conclusion

To get the body’s content of an iframe in JavaScript, we can use the iframe’s contentDocument property.

Categories
JavaScript Answers

How to round up a number in JavaScript?

Sometimes, we want to round up a number in JavaScript.

In this article, we’ll look at how to round up a number in JavaScript.

How to round up a number in JavaScript?

To round up a number in JavaScript, we can use the Math.round method with the toFixed method.

For instance, we write

const n = (Math.round(price * 10) / 10).toFixed(2);

to multiply price by 10.

Then we call Math.round to round the number.

And then we divide the rounded by 10 return the number rounded up to the nearest 10.

And then we call toFixed with 2 to return a string with the numbered rounded to 2 decimal places.

Conclusion

To round up a number in JavaScript, we can use the Math.round method with the toFixed method.