Categories
JavaScript Answers

How to convert positive number to negative number in JavaScript?

Sometimes, we want to convert positive number to negative number in JavaScript.

In this article, we’ll look at how to convert positive number to negative number in JavaScript.

How to convert positive number to negative number in JavaScript?

To convert positive number to negative number in JavaScript, we can use the - operator.

For instance, we write

const n = -Math.abs(num);

to call Math.abs with num to get the absolute value of num.

Then we use the unary - operator to convert the absolute value to a negative number.

Conclusion

To convert positive number to negative number in JavaScript, we can use the - operator.

Categories
JavaScript Answers

How to convert HH:MM:SS string to seconds only in JavaScript?

Sometimes, we want to convert HH:MM:SS string to seconds only in JavaScript.

In this article, we’ll look at how to convert HH:MM:SS string to seconds only in JavaScript.

How to convert HH:MM:SS string to seconds only in JavaScript?

To convert HH:MM:SS string to seconds only in JavaScript, we can split the time string.

And then we convert the hours and minutes to seconds and add them to the seconds.

For instance, we write

const hms = "02:04:33";
const [hours, minutes, secs] = hms.split(":");
const seconds = +hours * 60 * 60 + +minutes * 60 + +secs;
console.log(seconds);

to use hms.split(":") to split the string by the colons.

And then we convert the hours string to seconds with +hours * 60 * 60.

+ converts hours to a number.

Likewise, we convert minutes to seconds with +minutes * 60.

Then we add them both to seconds.

Conclusion

To convert HH:MM:SS string to seconds only in JavaScript, we can split the time string.

And then we convert the hours and minutes to seconds and add them to the seconds.

Categories
JavaScript Answers

How to set view width automatically by text inside with React Native?

Sometimes, we want to set view width automatically by text inside with React Native.

In this article, we’ll look at how to set view width automatically by text inside with React Native.

How to set view width automatically by text inside with React Native?

To set view width automatically by text inside with React Native, we set the alignSelf style.

For instance, we write

<View style={{ backgroundColor: "#000000", alignSelf: "flex-start" }}>
  <Text style={{ color: "#ffffff" }}>Sample text here</Text>
</View>

to set the alignSelf style of the View to 'flex-start' to make it display as an inline-block component.

An inline-block component will be sized according to the width of its contents.

Conclusion

To set view width automatically by text inside with React Native, we set the alignSelf style.

Categories
JavaScript Answers

How to create empty array of a given size with JavaScript?

Sometimes, we want to create empty array of a given size with JavaScript.

In this article, we’ll look at how to create empty array of a given size with JavaScript.

How to create empty array of a given size with JavaScript?

To create empty array of a given size with JavaScript, we use the Array constructor.

For instance, we write

const arr = Array(5);
console.log(arr.length);

to call Array with 5 to return an array with 5 empty slots.

Conclusion

To create empty array of a given size with JavaScript, we use the Array constructor.

Categories
JavaScript Answers

How to bind function arguments without binding this with JavaScript?

Sometimes, we want to bind function arguments without binding this with JavaScript.

In this article, we’ll look at how to bind function arguments without binding this with JavaScript.

How to bind function arguments without binding this with JavaScript?

To bind function arguments without binding this with JavaScript, we return a function that calls the function that we want to bind the arguments with.

For instance, we write

const bindArgs = (func, ...boundArgs) => {
  return (...args) => {
    return func(...boundArgs, ...args);
  };
};

const deleteGroup = bindArgs(deleteGroup, "groupName1");

to create the bindArgs function that returns a function that calls func with the arguments from the boundArgs array and the args that we pass into the function returned by bindArgs.

Then we call bindArgs with the deleteGroup and args being an array with 'groupName1'.

Conclusion

To bind function arguments without binding this with JavaScript, we return a function that calls the function that we want to bind the arguments with.