Categories
JavaScript Answers

How to Match a Whole Word in JavaScript?

Sometimes, we want to match a whole word in JavaScript.

In this article, we’ll look at how to match a whole word in JavaScript.

Match a Whole Word in JavaScript

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

For instance, we can write:

const lookup = '\n\n\n\n\n\nPC Games\n\n\n\n';
const word  = lookup.trim() ;
const match = new RegExp(`\\b${word}\\b`).test('2 PC Games')
console.log(match)

We create the word string with the words we’re looking for.

Then we create the regex with the RegExp constructor.

'\\b' is the word boundary for the pattern. This will let us match the whole word string.

Then we call test on the regex with the string we want to check if it matches the pattern.

Therefore, match should log true since the string matches the pattern.

Conclusion

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

Categories
JavaScript Answers

How to Get Paragraph Text Inside an Element with JavaScript?

Sometimes, we want to get paragraph text inside an element with JavaScript.

In this article, we’ll look at how to get paragraph text inside an element with JavaScript.

Get Paragraph Text Inside an Element with JavaScript

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

For instance, if we have:

<p>
  hello world
</p>

Then we write:

const text = document.querySelector('p').innerHTML;
console.log(text)

to get the p element with document.querySelector.

Then we use the innerHTML property to return the text.

Therefore, from the console log, we see that text is:

  hello world

Conclusion

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

Categories
JavaScript Answers

How to Calculate Date from Week Number in JavaScript?

Sometimes, we want to calculate date from week number in JavaScript.

In this article, we’ll look at how to calculate date from week number in JavaScript.

Calculate Date from Week Number in JavaScript

To calculate date from week number in JavaScript, we can use the JavaScript date setDate and getDate methods.

For instance, we can write:

const getSundayFromWeekNum = (weekNum, year) => {
  const sunday = new Date(year, 0, (1 + (weekNum - 1) * 7));
  while (sunday.getDay() !== 0) {
    sunday.setDate(sunday.getDate() - 1);
  }
  return sunday;
}

console.log(getSundayFromWeekNum(10, 2021))

We create the getSundayFromWeekNum function that takes the weekNum and year parameters that we use to get the Sunday of the given year and week number.

In the function, we create a JavaScript date with the Date constructor with year, 0, (1 + (weekNum - 1) * 7) to get the date given weekNum and year.

Then we add a while loop to subtract 1 day from the sundday date until we reach a Sunday.

Finally, we return that result.

Then when we console log, we get Sun Feb 28 2021 00:00:00 GMT-0800 (Pacific Standard Time).

Cconclusion

To calculate date from week number in JavaScript, we can use the JavaScript date setDate and getDate methods.

Categories
JavaScript Answers

How to Generate a 4 Digit Random Number with JavaScript?

Sometimes, we want to generate a 4 digit random number with JavaScript.

In this article, we’ll look at how to generate a 4 digit random number with JavaScript.

Generate a 4 Digit Random Number with JavaScript

To generate a 4 digit random number with JavaScript, we can use the Math.random method.

For instance, we can write:

const val = Math.floor(1000 + Math.random() * 9000);
console.log(val);

We call the Math.random method to generate a random number between 0 and 1.

Then we multiply that by 9000 to get a number between 0 and 9000.

Then to get a number between 0 and 10000, we add 1000 to the returned number.

Finally, we call Math.floor on the generated number to round it down to a number between 1000 and 9999.

Conclusion

To generate a 4 digit random number with JavaScript, we can use the Math.random method.

Categories
JavaScript Answers

How to Get the Month Name of Last Month using Moment.js and JavaScript?

Sometimes, we want to get the month name of last month using Moment.js and JavaScript.

In this article, we’ll look at how to get the month name of last month using Moment.js and JavaScript.

How to get the month name of last month using Moment.js and JavaScript?

To get the month name of last month using moment.js, we can use the subtract, startOf and format methods.

For instance, we can write:

const monthMinusOneName =  moment('2021-08-10').subtract(1, "month").startOf("month").format('MMMM');
console.log(monthMinusOneName)

We call moment to create moment date object.

Then we call subtract with 1 and 'month' to subtract 1 month from the moment date object.

Next, we call startOf with 'month' to return the moment date for the start of the subtracted date.

Finally, we call format with 'MMMM' to return the full month name.

Therefore, monthMinusOneName is 'July'.

Conclusion

To get the month name of last month using Moment.js and JavaScript., we can use the subtract, startOf and format methods.