Categories
JavaScript Answers

How to make a loading animation in console application written in JavaScript and Node.js?

Sometimes, we want to make a loading animation in console application written in JavaScript and Node.js.

In this article, we’ll look at how to make a loading animation in console application written in JavaScript and Node.js.

How to make a loading animation in console application written in JavaScript and Node.js?

To make a loading animation in console application written in JavaScript and Node.js, we can use timers and the process.stdout.write method to write the loading animation characters to the screen.

For instance, we write:

const P = ['\\', '|', '/', '-'];
let x = 0;
const loader = setInterval(() => {
  process.stdout.write(`\r${P[x++]}`);
  x %= P.length;
}, 250);

setTimeout(() => {
  clearInterval(loader);
}, 5000);

We call setInterval with a callback that calls process.stdout.write to write the characters in P with index x to the screen every 250 seconds.

Then we call setTimeout with a callback to clear the loader timer in 5 seconds.

Conclusion

To make a loading animation in console application written in JavaScript and Node.js, we can use timers and the process.stdout.write method to write the loading animation characters to the screen.

Categories
JavaScript Answers

How to set options of a dropdown using JavaScript?

Sometimes, we want to set options of a dropdown using JavaScript.

In this article, we’ll look at how to set options of a dropdown using JavaScript.

How to set options of a dropdown using JavaScript?

To set options of a dropdown using JavaScript, we can set the value property of the select element.

For instance, we write:

<select>
  <option value='apple'>Apple</option>
  <option value='orange'>Orange</option>
  <option value='grape'>Grape</option>
</select>

to add a select element.

Then we write:

const select = document.querySelector("select")
select.value = "orange";

to select the select element with querySelector.

And then we set select.value to 'orange' to select the 2nd choice.

We set value to a value that of value attribute in the option element.

Conclusion

To set options of a dropdown using JavaScript, we can set the value property of the select element.

Categories
JavaScript Answers

How to remove all properties from object except specified key with JavaScript?

Sometimes, we want to remove all properties from object except specified key with JavaScript.

In this article, we’ll look at how to remove all properties from object except specified key with JavaScript.

How to remove all properties from object except specified key with JavaScript?

To remove all properties from object except specified key with JavaScript, we can use some object and array methods.

For instance, we write:

const validKeys = ['a', 'b', 'c'];
const userInput = {
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5
}

const filteredEntries = Object.entries(userInput).filter(([key]) => validKeys.includes(key))
const newUserInput = Object.fromEntries(filteredEntries)
console.log(newUserInput)

to call Object.entries to return an array of key-value pairs in userInput as a nested array.

Then we call filter with a callback to return the key-value pair arrays with key that’s in validKeys.

Next, we call Object.fromEntries with filteredEntries to return the object created from the key-value pair arrays in filteredEntries.

Therefore, newUserInput is:

{
  a: 1,
  b: 2,
  c: 3
}

Conclusion

To remove all properties from object except specified key with JavaScript, we can use some object and array methods.

Categories
JavaScript Answers

How to refresh the screen on browser resize with JavaScript?

Sometimes, we want to refresh the screen on browser resize with JavaScript.

In this article, we’ll look at how to refresh the screen on browser resize with JavaScript.

How to refresh the screen on browser resize with JavaScript?

To refresh the screen on browser resize with JavaScript, we can watch the resize event on the window and call location.reload in the resize handler.

For instance, we write:

window.onresize = () => {
  location.reload();
}

to set window.onresize to a function that runs when we resize the browser window or tab.

In it, we run location.reload to refresh the page.

Therefore, when we resize the window, the page is refreshed.

Conclusion

To refresh the screen on browser resize with JavaScript, we can watch the resize event on the window and call location.reload in the resize handler.

Categories
JavaScript Answers

How to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript?

Sometimes, we want to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript.

In this article, we’ll look at how to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript.

How to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript?

To get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript, we can use some date methods.

For instance, we write:

const startAndEndOfWeek = (date) => {
  const now = date ? new Date(date) : new Date().setHours(0, 0, 0, 0);
  const monday = new Date(now);
  monday.setDate(monday.getDate() - monday.getDay() + 1);
  const sunday = new Date(now);
  sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
  return [monday, sunday];
}

const d = new Date(2022, 3, 1)
console.log(startAndEndOfWeek(d))

to define the startAndEndOfWeek function that takes a date.

In it, we compute the Monday and Sunday of the week that has the given date.

To do this, we create a copy of date with the Date constructor.

Then we get monday by creating a new date from now.

And then we call setDate with monday.getDate() - monday.getDay() + 1 to set the day of the week of monday to the Monday within the same week as date.

Likewise, we call setDate with sunday.getDate() - sunday.getDay() + 7 to set the day of the week of sunday to the Sunday within the same week as date.

Finally, we return both dates.

As a result, the console log logs  [Mon Mar 28 2022 00:00:00 GMT-0700 (Pacific Daylight Time), Sun Apr 03 2022 00:00:00 GMT-0700 (Pacific Daylight Time)].

Conclusion

To get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript, we can use some date methods.