Categories
JavaScript Answers

How to validate date with format mm/dd/yyyy in JavaScript?

Sometimes, we want to validate date with format mm/dd/yyyy in JavaScript.

In this article, we’ll look at how to validate date with format mm/dd/yyyy in JavaScript.

How to validate date with format mm/dd/yyyy in JavaScript?

To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.

For instance, we write

console.log(moment("05/22/2022", "MM/DD/YYYY", true).isValid());

to check that "05/22/2022" matches the "MM/DD/YYYY" format with moment.

We call moment with true to make sure the date string matches the date format in the 2nd argument exactly.

Then we call isValid to return whether the date string is valid in the given format or not.

Conclusion

To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.

Categories
JavaScript Answers

How to record audio to file with JavaScript?

Sometimes, we want to record audio to file with JavaScript.

In this article, we’ll look at how to record audio to file with JavaScript.

How to record audio to file with JavaScript?

To record audio to file with JavaScript, we use the MediaRecorder API.

For instance, we write

let audioChunks = [];
let rec;

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
  handlerFunction(stream);
});

const sendData = (blob) => {
  //...
};
const handlerFunction = (stream) => {
  rec = new MediaRecorder(stream);
  rec.ondataavailable = (e) => {
    audioChunks.push(e.data);
    if (rec.state == "inactive") {
      let blob = new Blob(audioChunks, { type: "audio/mp3" });
      recordedAudio.src = URL.createObjectURL(blob);
      recordedAudio.controls = true;
      recordedAudio.autoplay = true;
      sendData(blob);
    }
  };
};

record.onclick = (e) => {
  record.disabled = true;
  record.style.backgroundColor = "blue";
  stopRecord.disabled = false;
  audioChunks = [];
  rec.start();
};

stopRecord.onclick = (e) => {
  record.disabled = false;
  stop.disabled = true;
  record.style.backgroundColor = "red";
  rec.stop();
};

to check if we have audio permission with

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
  handlerFunction(stream);
});

If we have then we call handlerFunction to handle the stream. We record the audio by calling rec.start, where rec is a MediaRecorder instance we created with the stream.

And in the stopRecord.onclick method, we call rec.stop to stop recording.

Then if we check that rec.state is 'inactive', we create an mp3 blob from the recorded audio with

let blob = new Blob(audioChunks,{type:'audio/mp3'});

Then we can set URL.createObjectURL(blob); as the src property of the recordedAudio audio element so we can play the recorded audio.

Conclusion

To record audio to file with JavaScript, we use the MediaRecorder API.

Categories
JavaScript Answers

How to change an element’s text without changing its child elements with JavaScript?

Sometimes, we want to change an element’s text without changing its child elements with JavaScript.

In this article, we’ll look at how to change an element’s text without changing its child elements with JavaScript.

How to change an element’s text without changing its child elements with JavaScript?

To change an element’s text without changing its child elements with JavaScript, we set the nodeValue property of the text node.

For instance, we write

const div = document.getElementById("foo");
div.firstChild.nodeValue = "New text";

to select the element with getElementById.

Then we get its first child node with firstChild.

And then we set its content by setting the nodeValue to a new value.

Conclusion

To change an element’s text without changing its child elements with JavaScript, we set the nodeValue property of the text node.

Categories
JavaScript Answers

How to check whether something is iterable in JavaScript?

Sometimes, we want to check whether something is iterable in JavaScript.

In this article, we’ll look at how to check whether something is iterable in JavaScript.

How to check whether something is iterable in JavaScript?

To check whether something is iterable in JavaScript, we can check if the Symbol.iterator property of the object if a function.

For instance, we write

const isIterable = (obj) => {
  if (obj === null || obj === undefined) {
    return false;
  }
  return typeof obj[Symbol.iterator] === "function";
};

to check if obj is null or undefined with

obj === null || obj === undefined

If it is, then we return false.

Otherwise, we check if the obj‘s Symbol.iterator property is a function with

typeof obj[Symbol.iterator] === "function"

If that’s true, then obj is an iterable object.

Conclusion

To check whether something is iterable in JavaScript, we can check if the Symbol.iterator property of the object if a function.

Categories
JavaScript Answers

How to convert binary string to decimal with JavaScript?

Sometimes, we want to convert binary string to decimal with JavaScript.

In this article, we’ll look at how to convert binary string to decimal with JavaScript.

How to convert binary string to decimal with JavaScript?

To convert binary string to decimal with JavaScript, we can use the parseInt function.

For instance, we write

const binary = "1101000";
const digit = parseInt(binary, 2);
console.log(digit);

to call parseInt with the binary binary string and 2 to return the binary string parsed into a decimal number.

Conclusion

To convert binary string to decimal with JavaScript, we can use the parseInt function.