Categories
JavaScript Answers

How to convert numbers between different bases in JavaScript?

Sometimes, we want to convert numbers between different bases in JavaScript.

In this article, we’ll look at how to convert numbers between different bases in JavaScript.

How to convert numbers between different bases in JavaScript?

To convert numbers between different bases in JavaScript, we can use the parseInt function.

For instance, we write

const n = parseInt(string, radix)

to call parseInt to convert the number string into the base specified by the radix.

Conclusion

To convert numbers between different bases in JavaScript, we can use the parseInt function.

Categories
JavaScript Answers

How to reset the navigation stack for the home screen with React Navigation and React Native?

Sometimes, we want to reset the navigation stack for the home screen with React Navigation and React Native.

In this article, we’ll look at how to reset the navigation stack for the home screen with React Navigation and React Native.

How to reset the navigation stack for the home screen with React Navigation and React Native?

To reset the navigation stack for the home screen with React Navigation and React Native, we can use the navigation.dispatch and the CommonActions.reset methods.

For instance, we write

import { CommonActions } from "@react-navigation/native";

navigation.dispatch(
  CommonActions.reset({
    index: 1,
    routes: [
      { name: "Home" },
      {
        name: "Profile",
        params: { user: "jane" },
      },
    ],
  })
);

to call navigation.dispatch with the object returned by the CommonActions.reset method.

We call CommonActions.reset with an object that resets the navigation state of the app to the have the Home and Profile routes.

The existing navigation state is replaced with the items we called CommonActions.reset with.

Conclusion

To reset the navigation stack for the home screen with React Navigation and React Native, we can use the navigation.dispatch and the CommonActions.reset methods.

Categories
JavaScript Answers

How to override methods with JavaScript?

Sometimes, we want to override methods with JavaScript.

In this article, we’ll look at how to override methods with JavaScript.

How to override methods with JavaScript?

To override methods with JavaScript, we can use the class syntax.

For instance, we write

class A {
  speak() {
    console.log("I'm A");
  }
}

class B extends A {
  speak() {
    super.speak();
    console.log("I'm B");
  }
}

const a = new A();
a.speak();

const b = new B();
b.speak();

to create class B that inherits from class A.

In it, we override class A‘s speak method by calling super.speak.

Then we add our own behavior for the speak method exclusive to B.

Therefore, when we call a.speak, we get "I'm A".

And when we call b.speak, we get "I'm B".

Categories
JavaScript Answers

How to convert milliseconds to minutes and seconds with JavaScript?

Sometimes, we want to convert milliseconds to minutes and seconds with JavaScript.

In this article, we’ll look at how to convert milliseconds to minutes and seconds with JavaScript.

How to convert milliseconds to minutes and seconds with JavaScript?

To convert milliseconds to minutes and seconds with JavaScript, we can use some date methods.

For instance, we write

const d = new Date(Date.UTC(0, 0, 0, 0, 0, 0, 298999));

const parts = [d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()];
const formatted = parts.map((s) => String(s).padStart(2, "0")).join(":");

to create a UTC date with the Date.UTC method and the Date constructor.

Then we put the hours, minutes, and seconds of the d date into the parts array.

getUTCHours gets the hours in UTC.

getUTCMinutes gets the minutes in UTC.

getUTCSeconds gets the seconds in UTC.

Then we prepend a 0 to each number if it has less than 2 digits with padStart after converting them to strings with String.

And we join the strings together with join.

Conclusion

To convert milliseconds to minutes and seconds with JavaScript, we can use some date methods.

Categories
JavaScript Answers

How to do a greater than or equal to with moment.js in JavaScript?

Sometimes, we want to do a greater than or equal to with moment.js in JavaScript.

In this article, we’ll look at how to do a greater than or equal to with moment.js in JavaScript.

How to do a greater than or equal to with moment.js in JavaScript?

To do a greater than or equal to with moment.js in JavaScript, we can use the isSameOrAfter method.

For instance, we write

moment1.isSameOrAfter(moment2);

to check if the datetime in the moment1 is the same or after the datetime in the moment2 moment object.

Conclusion

To do a greater than or equal to with moment.js in JavaScript, we can use the isSameOrAfter method.