skip to Main Content

Okay hear me out, i know this might be a dumb question and there is probably a nice easy solution to this, but english is not my native language and i can’t for the love of me find out what to even search on google or here for this problem.

The gist of it is, that i have a really simple discord bot running on Nodejs with Discord.js V14.
I have a const that is defined as an integer. I want to convert this integer to a "powernumber" (these: ³ ¹ ⁴)

Is there any way this is even possible in a clean way?

I didn’t really try anything yet, since i don’t even know where to start. But what im trying to do is basically this

const number = "3"

//some node js magic that converts ³ to ³

if the ³ gets output as a string, that would be optimal, since i want to use that in a nickname like for example:

const user = interaction.options.getMember('user')

user.setNickname('nickname${number}'

I again appologise for not being able to explain exactly what i want. As i said earlier, english is not my native language :/

2

Answers


  1. My understanding of what you are trying to do here is convert integers string characters into superscript string characters? So the character '1' becomes the string '¹'.

    If you are only working with numbers 1 to 9 the easiest solution will be to create a map using a simple object.

    const superscriptMap = {
    "1": '¹',
    "2": 
    

    And so forth will all the numbers.
    Then use it like so:

    const superscriptNumber = superscriptMap[normalNumber];
    

    Watch out that if normalNumber is not in your object then you will get undefined so you might want to handle that if it happens.

    Thanks to @DallogFheir for helping me spot it was string’s and not integers!

    Login or Signup to reply.
  2. You can use replace with a callback function that will read out the superscript from a string:

    const turnDigitsToSuperscript = (s) => s.replace(/d/g, d => "⁰¹²³⁴⁵⁶⁷⁸⁹"[d]);
    
    // Example:
    const s = "test123"
    console.log(turnDigitsToSuperscript(s));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search