Categories
JavaScript Answers

How to create a unique number with JavaScript time?

Sometimes, we want to create a unique number with JavaScript time.

In this article, we’ll look at how to create a unique number with JavaScript time.

How to create a unique number with JavaScript time?

To create a unique number with JavaScript time, we can add the timestamp of the current datetime to a random number.

For instance, we write

const n = Date.now() + Math.random();

to add the timestamp of the current datetime that we get with Date.now to a random number returned by Math.random.

Conclusion

To create a unique number with JavaScript time, we can add the timestamp of the current datetime to a random number.

Categories
JavaScript Answers

How to select option in drop down Protractor e2e tests with JavaScript?

Sometimes, we want to select option in drop down Protractor e2e tests with JavaScript.

In this article, we’ll look at how to select option in drop down Protractor e2e tests with JavaScript.

How to select option in drop down Protractor e2e tests with JavaScript?

To select option in drop down Protractor e2e tests with JavaScript, we can select the select element and then call click on it.

For instance, we write

element(by.cssContainingText("option", "BeaverBox Testing")).click();

to select the option element that we want to pick with

element(by.cssContainingText("option", "BeaverBox Testing"))

cssContainingText lets us select by option element’s text content.

Then we call click on click on the option to select it.

Conclusion

To select option in drop down Protractor e2e tests with JavaScript, we can select the select element and then call click on it.

Categories
JavaScript Answers

How to get the raw value a number input field with JavaScript?

Sometimes, we want to get the raw value a number input field with JavaScript.

In this article, we’ll look at how to get the raw value a number input field with JavaScript.

How to get the raw value a number input field with JavaScript?

To get the raw value a number input field with JavaScript, we can select the input content and then get the selected contents.

For instance, we write

input.focus();
document.execCommand("SelectAll");
const displayValue = window.getSelection().toString();

to call focus to focus on the input.

Then we call execCommand with 'SelectAll' to select the input box contents.

Finally, we get the selection as a string with window.getSelection().toString().

Conclusion

To get the raw value a number input field with JavaScript, we can select the input content and then get the selected contents.

Categories
JavaScript Answers

How to watch for array changes with JavaScript?

Sometimes, we want to watch for array changes with JavaScript.

In this article, we’ll look at how to watch for array changes with JavaScript.

How to watch for array changes with JavaScript?

To watch for array changes with JavaScript, we use the underscore-observe library.

We use it by installing Lodash or Underscore.

Then we add contents of the https://github.com/mennovanslooten/underscore-observe/blob/master/underscore-observe.js file into our code.

Then we watch an array for changes by writing

const a = ["zero", "one", "two", "trhee"];

_.observe(a, () => {
  alert("something happened");
});

We call observe with array a and a callback that runs when a changes.

Conclusion

To watch for array changes with JavaScript, we use the underscore-observe library.

Categories
JavaScript Answers

How to check time difference in JavaScript?

Sometimes, we want to check time difference in JavaScript.

In this article, we’ll look at how to check time difference in JavaScript.

How to check time difference in JavaScript?

To check time difference in JavaScript, we can use the date getTime method.

For instance, we write

const date1 = new Date("08/05/2022 23:41:20");
const date2 = new Date("08/06/2022 02:56:32");

const diff = date2.getTime() - date1.getTime();

let msec = diff;
const hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
const mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
const ss = Math.floor(msec / 1000);
msec -= ss * 1000;

to call getTime to get the timestamp of the dates in milliseconds.

Then we get the difference of the timestamps.

Next, we get the hours with Math.floor(msec / 1000 / 60 / 60).

Then we subtract that from msec and we use the new msec value to get the minutes difference with Math.floor(msec / 1000 / 60).

Likewise, we use the same steps to get the seconds difference and we get the seconds difference with Math.floor(msec / 1000).

Conclusion

To check time difference in JavaScript, we can use the date getTime method.