Categories
JavaScript Answers

How to get the day of the week from the day number in JavaScript?

To get the day of the week from the day number in JavaScript, we use the date getDay method.

For instance, we write

const dayOfTheWeek = (day, month, year) => {
  const weekday = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
  ];
  return weekday[new Date(`${month}/${day}/${year}`).getDay()];
};

console.log(dayOfTheWeek(3, 11, 2022));

to define the dayOfTheWeek function.

In it, we put the month, day and year into a string to create a Date object from it.

Then we call getDay to get the day of the week with 0 being Sunday, 1 being Monday, etc.

We use that returned number as the index of weekday to get the day of the week.

Categories
JavaScript Answers

How to get the day of the week from the day number in JavaScript?

To get the day of the week from the day number in JavaScript, we call the toLocaleString method.

For instance, we write

const weekday = new Date().toLocaleString("en-us", { weekday: "long" });

to call toLocaleString on a date object with the locale and an object specifying the date string returned.

We set weekday to 'long' to return the day of the week.

Categories
JavaScript Answers

How to send a PDF file directly to the printer using JavaScript?

To send a PDF file directly to the printer using JavaScript, we create an object URL.

For instance, we write

const dataUrl = window.URL.createObjectURL(file);
const pdfWindow = window.open(dataUrl);
pdfWindow.print();

to call createObjectURL with the PDF file to create the PDF file object URL string.

Then we open the dataUrl in a window wiith window.open.

Finally, we call print to open the print dialog to print the PDF.

Categories
JavaScript Answers

How to find out how many times an array element appears with JavaScript?

To find out how many times an array element appears with JavaScript, we use the filter method.

For instance, we write

const countInArray = (array, what) => {
  return array.filter((item) => item == what).length;
};

to define the countInArray function.

In it, we get the count the number of items equal to what by calling array.filter with a callback that returns if item equals to what to return an array with entries with what inside.

Then we get the length property to get the count of what in the array returned by filter.

Categories
JavaScript Answers

How to update the attribute value of an object using the map function in JavaScript?

To update the attribute value of an object using the map function in JavaScript, we return the updated object.

For instance, we write

const editSchoolName = (schools, oldName, name) =>
  schools.map((item) => {
    if (item.name === oldName) {
      return { ...item, name };
    } else {
      return item;
    }
  });

to define the editSchooName function that returns a new version of the schools array that has entries mapped from schools with the objects updated if they have name property equal to oldName.