Categories
HTML

How to change the interval time on Bootstrap carousel with HTML?

To change the interval time on Bootstrap carousel with HTML, we set the data-interval attribute.

For instance, we write

<div
  data-ride="carousel"
  class="carousel slide"
  data-interval="10000"
  id="myCarousel"
>
  ...
</div>

to set the data-interval attribute to 10000ms to make the carousel update every 10000ms.

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.

Categories
JavaScript Answers

How to move to prev/next element of an array with JavaScript?

To move to prev/next element of an array with JavaScript, we can define our own array class.

For instance, we write

class TraversableArray extends Array {
  next() {
    return this[++this.current];
  }
}

to define the TraversableArray class that extends the Array class.

In it, we add the next method that returns the object at the current index after incrementing it by 1.