convert a number from base 10 to base 8 in javascript

To convert a number from base 10 to base 8 in JavaScript, you can use the parseInt() function with a radix of 8. The radix specifies the base of the number system to use when parsing the string. Here's an example implementation:

index.tsx
function base10ToBase8(num) {
  return parseInt(num, 10).toString(8);
}
72 chars
4 lines

In this function, num is the number to convert. We first parse it as a base 10 integer using parseInt(). We then call toString(8) on the resulting number to convert it to base 8.

Here's an example usage:

index.tsx
var num = 10;
var base8Num = base10ToBase8(num);
console.log(base8Num); // Output: "12"
88 chars
4 lines

In this example, the number 10 is converted to base 8, which results in the number 12. The console.log() call outputs this number to the console.

gistlibby LogSnag