Categories
JavaScript Answers

How to convert string into array of integers with JavaScript?

Sometimes, we want to convert string into array of integers with JavaScript.

In this article, we’ll look at how to convert string into array of integers with JavaScript.

How to convert string into array of integers with JavaScript?

To convert string into array of integers with JavaScript, we can use the string split and array map methods.

For instance, we write

const arr = "14 2".split(" ").map(Number);

to call split with ' ' to split the string by the spaces into an array of substrings.

Then we call map with Number to convert each substring into a number and return a new array with the numbers.

Conclusion

To convert string into array of integers with JavaScript, we can use the string split and array map methods.

Categories
JavaScript Answers

How to use form input to access camera and immediately upload photos using web app with JavaScript?

Sometimes, we want to use form input to access camera and immediately upload photos using web app with JavaScript.

In this article, we’ll look at how to use form input to access camera and immediately upload photos using web app with JavaScript.

How to use form input to access camera and immediately upload photos using web app with JavaScript?

To use form input to access camera and immediately upload photos using web app with JavaScript, we can set the accept attribute of the file input to image/*;capture=camera.

For instance, we write

<input id="myFileInput" type="file" accept="image/*;capture=camera" />

to add a file input that accepts photos captured from a camera.

Then we write

const myInput = document.getElementById("myFileInput");

const sendPic = () => {
  const file = myInput.files[0];
  //...
};

myInput.addEventListener("change", sendPic, false);

to select the file input with getElementById.

Then we call addEventListener to listen to the change event triggered by the file input.

We call sendPic to get the image from myInput.files[0].

And then we can do what we want with it.

Conclusion

To use form input to access camera and immediately upload photos using web app with JavaScript, we can set the accept attribute of the file input to image/*;capture=camera.

Categories
JavaScript Answers

How to get data out of a Node.js HTTP get request with JavaScript?

Sometimes, we want to get data out of a Node.js HTTP get request with JavaScript.

In this article, we’ll look at how to get data out of a Node.js HTTP get request with JavaScript.

How to get data out of a Node.js HTTP get request with JavaScript?

To get data out of a Node.js HTTP get request with JavaScript, we can listen for the data event on the response.

For instance, we write

const callback = (response) => {
  let str = "";
  response.on("data", (chunk) => {
    str += chunk;
  });

  response.on("end", () => {
    console.log(str);
    // ...
  });
};

const req = http.request(options, callback).end();

to call http.request with the request options and a callback that calls response.one to listen for the 'data' event.

In the data event callback, we get the response chunk which has a chunk of the response body string.

We concatenate all the chunks into str so we get the full response body string.

When we get all the chunks, the end event is emitted and we log the value of str in the callback.

Conclusion

To get data out of a Node.js HTTP get request with JavaScript, we can listen for the data event on the response.

Categories
JavaScript Answers

How to subtract one month using moment.js and JavaScript?

Sometimes, we want to subtract one month using moment.js and JavaScript.

In this article, we’ll look at how to subtract one month using moment.js and JavaScript.

How to subtract one month using moment.js and JavaScript?

To subtract one month using moment.js and JavaScript, we can use the subtract method.

For instance, we write

const d = moment().subtract(1, "months").format("MMM YYYY");

to create a moment object with the current datetime with moment.

Then we call subtract with 1 and 'months' to subtract 1 month from the current date time in the moment object and return it.

And then we call format to return a formatted date string with the month and year.

Conclusion

To subtract one month using moment.js and JavaScript, we can use the subtract method.

Categories
JavaScript Answers

How to pass an unknown number of arguments into JavaScript function?

Sometimes, we want to pass an unknown number of arguments into JavaScript function.

In this article, we’ll look at how to pass an unknown number of arguments into JavaScript function.

How to pass an unknown number of arguments into JavaScript function?

To pass an unknown number of arguments into JavaScript function, we use the rest operator.

For instance, we write

const printNames = (...names) => {
  for (const name of names) {
    console.log(name);
  }
};

to define the printNames function that takes an infinite number of arguments.

We use the ... rest operator to get all the arguments into the names array.

Then we use a for-of loop to loop through all the names entries and print out all the entries into the console.

Conclusion

To pass an unknown number of arguments into JavaScript function, we use the rest operator.