Categories
JavaScript Answers

How to 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.

Categories
JavaScript Answers

How to Get Computed Styles of an Element with JavaScript?

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.

For instance, if we have:

<div>
  hello
</div>

Then we write:

const element = document.querySelector('div')
const style = window.getComputedStyle(element)
console.log(style)

We get the div with document.querySelector.

Then we call window.getComputedStyle with element as the argument.

Therefore, the console log should show all the computed CSS properties with their values for the div.

Categories
JavaScript Answers

How to 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.

Categories
JavaScript Answers

How to get the Month Name of Last Month using Moment.js?

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'.

Categories
JavaScript Answers

How to Remove an Object from an Array of Objects in JavaScript?

To remove an object from an array of objects in JavaScript, we can use the JavaScript array’s findIndex method to get the index of the first element that matches the given condition.

Then we can use the JavaScript array’s splice method to remove the item from the list.

For instance, we can write:

const arr = [{
  "value": "14",
  "label": "7"
}, {
  "value": "14",
  "label": "7"
}, {
  "value": "18",
  "label": "7"
}]


const matchesEl = (el) => {
  return el.value === '14' && el.label === '7';
}

arr.splice(arr.findIndex(matchesEl), 1);
console.log(arr)

We have the arr array with the object’s we want to remove.

Then we have the matchesEl fuinction that returns the boolean expression that matches an item where el.value is 14 and el.label is 7.

Next, we call arr.findIndex with matchesEl to return the index of the first object that has value 14 and label 7 in `arr.

Then we call arr.splice with the returned index and 1 to remove that entry from arr.

Therefore, arr is now:

[
  {
    "value": "14",
    "label": "7"
  },
  {
    "value": "18",
    "label": "7"
  }
]