Categories
JavaScript Answers

How to count words in string with JavaScript?

Sometimes, we want to count words in string with JavaScript.

In this article, we’ll look at how to count words in string with JavaScript.

How to count words in string with JavaScript?

To count words in string with JavaScript, we can use the string split method.

For instance, we write

const numWords = string.split(/\s+/).length;

to call string.split with the /\s+/ regex to split the string string by the spaces.

It returns an array with the split strings.

So we get the number of words in the string with the length property.

Conclusion

To count words in string with JavaScript, we can use the string split method.

Categories
JavaScript Answers

How to show current time in JavaScript in the format HH:MM:SS?

Sometimes, we want to show current time in JavaScript in the format HH:MM:SS.

In this article, we’ll look at how to show current time in JavaScript in the format HH:MM:SS.

How to show current time in JavaScript in the format HH:MM:SS?

To show current time in JavaScript in the format HH:MM:SS, we use the date’s toLocaleTimeString method.

For instance, we write

const d = new Date();
console.log(d.toLocaleTimeString());

to create date d with the current datetime.

And we call toLocaleTimeString to return a string with the human readable locale time string with date and time being the values in d.

Conclusion

To show current time in JavaScript in the format HH:MM:SS, we use the date’s toLocaleTimeString method.

Categories
JavaScript Answers

How to format current time with JavaScript?

Sometimes, we want to format current time with JavaScript.

In this article, we’ll look at how to format current time with JavaScript.

How to format current time with JavaScript?

To format current time with JavaScript, we call toLocaleString, toLocaleDateString or toLocaleTimeString.

For instance, we write

const d = new Date();
console.log(d.toLocaleString());
console.log(d.toLocaleDateString());
console.log(d.toLocaleTimeString());

to create date d with the current datetime.

And then we call toLocaleString to return a string with the human readable locale datetime string with date and time being the values in d.

We call toLocaleDateString to return a string with the human readable locale date string with date and time being the values in d.

And we call toLocaleTimeString to return a string with the human readable locale time string with date and time being the values in d.

Conclusion

To format current time with JavaScript, we call toLocaleString, toLocaleDateString or toLocaleTimeString.

Categories
JavaScript Answers

How to have different return values for multiple calls on a Jasmine spy with JavaScript?

Sometimes, we want to have different return values for multiple calls on a Jasmine spy with JavaScript.

In this article, we’ll look at how to have different return values for multiple calls on a Jasmine spy with JavaScript.

How to have different return values for multiple calls on a Jasmine spy with JavaScript?

To have different return values for multiple calls on a Jasmine spy with JavaScript, we call returnValues with all the values we want to return.

For instance, we write

describe("A spy, when configured to fake a series of return values", () => {
  beforeEach(function () {
    spyOn(util, "foo").and.returnValues(true, false);
  });

  it("when called multiple times returns the requested values in order", () => {
    expect(util.foo()).toBeTruthy();
    expect(util.foo()).toBeFalsy();
    expect(util.foo()).toBeUndefined();
  });
});

to mock the util.foo method with spyOn in the beforeEach hook to create the mock before each test.

And then we call returnValues with all the values we want to return.

Then in the test, we call expect to check for each value returned by util.foo.

Conclusion

To have different return values for multiple calls on a Jasmine spy with JavaScript, we call returnValues with all the values we want to return.

Categories
JavaScript Answers

How to get the text node of an element with JavaScript?

Sometimes, we want to get the text node of an element with JavaScript.

In this article, we’ll look at how to get the text node of an element with JavaScript.

How to get the text node of an element with JavaScript?

To get the text node of an element with JavaScript, we call array find to find the text node in a node.

For instance, we write

const extract = (node) => {
  const text = [...node.childNodes].find(
    (child) => child.nodeType === Node.TEXT_NODE
  );
  return text?.textContent?.trim();
};

to create the extract function that takes the node and get the child nodes with node.childNodes.

Then we spread the child nodes into an array.

And we call find with a callback that gets the nodeType with type Node.TEXT_NODE.

Finally, we get the textContent of the text node.

Conclusion

To get the text node of an element with JavaScript, we call array find to find the text node in a node.