Categories
JavaScript Answers

How to generate range of numbers from 0 to n in JavaScript ES2015 only?

Sometimes, we want to generate range of numbers from 0 to n in JavaScript ES2015 only.

In this article, we’ll look at how to generate range of numbers from 0 to n in JavaScript ES2015 only.

How to generate range of numbers from 0 to n in JavaScript ES2015 only?

To generate range of numbers from 0 to n in JavaScript ES2015 only, we can use the array keys method.

For instance, we write

const nums = [...Array(n).keys()];

to use Array(n) to get an array with n empty slots.

Then we call keys on it to get the indexes of the array in an array.

Next, we use the spread operator to spread the indexes into a new array.

Conclusion

To generate range of numbers from 0 to n in JavaScript ES2015 only, we can use the array keys method.

Categories
JavaScript Answers

How to use regex in JavaScript to return just numbers?

Sometimes, we want to use regex in JavaScript to return just numbers.

In this article, we’ll look at how to use regex in JavaScript to return just numbers.

How to use regex in JavaScript to return just numbers?

To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.

For instance, we write

const numberPattern = /\d+/g;
const numbers = "something102asdfkj1948948".match(numberPattern);

to call match with the numberPattern regex to match all numbers in the string 'something102asdfkj1948948'.

And then we get the results returned as an array.

Conclusion

To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.

Categories
JavaScript Answers

How to concatenate regex literals in JavaScript?

Sometimes, we want to concatenate regex literals in JavaScript.

In this article, we’ll look at how to concatenate regex literals in JavaScript.

How to concatenate regex literals in JavaScript?

To concatenate regex literals in JavaScript, we can get the regex pattern with the source property and then we get the flags with various properties.

For instance, we write

const r1 = /abc/g;
const r2 = /def/;
const r3 = new RegExp(
  r1.source + r2.source,
  (r1.global ? "g" : "") +
    (r1.ignoreCase ? "i" : "") +
    (r1.multiline ? "m" : "")
);

to use the source property to get the /abc/ and /def patterns and concatenate them.

And then we use the global, ignoreCase, and multline to get those flags from r1.

Then we concatenate the patterns and flags together and put them in the RegExp constructor.

Conclusion

To concatenate regex literals in JavaScript, we can get the regex pattern with the source property and then we get the flags with various properties.

Categories
JavaScript Answers

How to pass parameter to a promise function with JavaScript?

Sometimes, we want to pass parameter to a promise function with JavaScript.

In this article, we’ll look at how to pass parameter to a promise function with JavaScript.

How to pass parameter to a promise function with JavaScript?

To pass parameter to a promise function with JavaScript, we create a function that takes the parameter and returns the promise.

For instance, we write

const logUser = (username, password) => {
  return new Promise((resolve, reject) => {
    //...
    const response = "user logged in";
    resolve(response);
  });
};

logUser("user", "pass").then((respFromLogUser) => {
  showMessage(respFromLogUser);
});

to create the logUser function that returns a promise that uses the username and password parameters.

Then we call logInUser with the arguments and call then with a callback that has the resolved value set as the value of the callback parameter.

Conclusion

To pass parameter to a promise function with JavaScript, we create a function that takes the parameter and returns the promise.

Categories
JavaScript Answers

How to detect shift+enter and generate a new line in text area with JavaScript?

Sometimes, we want to detect shift+enter and generate a new line in text area with JavaScript.

In this article, we’ll look at how to detect shift+enter and generate a new line in text area with JavaScript.

How to detect shift+enter and generate a new line in text area with JavaScript?

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.

For instance, we write

textArea.onKeyPress = (e) => {
  if (e.keyCode === 13 && e.shiftKey) {
    e.preventDefault();
    let start = e.target.selectionStart,
      end = e.target.selectionEnd;
    e.target.vaiue =
      prevState.message.substring(0, start) +
      "\n" +
      prevState.message.substring(end);
  } else if (e.keyCode === 13) {
    e.preventDefault();
  }
};

to check keyCode and shiftKey to see if shift+enter is pressed.

If it is, we call preventDefault to prevent the default behavior.

We then get the cursor’s start and end indexes with selectionStart and selectionEnd.

Then we put the new line in between the start and end with

prevState.message.substring(0, start) + "\n" + prevState.message.substring(end);

Conclusion

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object.