Counting digits of a number

Hey guys and ladies, today I ran into a problem using <before eval> inside a skill(rpg maker mv) using skill core plugin from yanfly, of course.
My problem is relative of a lack in competences about javascript where I must compact a script that read every target statistic(example target.hp, target.mhp) and count digits: for every digit put an X as an hidden tips of what a player can "see" as stats for a monster, like a scan from FFantasy series.

I forgot to mention that i'm using the trick from yanfly and his same example of a scan(he called Libra) relative to a monster reading.

my problem is about compacting the script cause basically it result too big and rude, how can I can let the machine to check every stat, counting digits and then draw X for each?

example monster stats;
1234 life, 100mp
10atk 100 def

scan skill reading:

life: xxxx mp: xxx
atk: xx def: xxx


more than resolving this, I want to learn the fundamental of every step to understand how to move in similar situations.

thanks even for reading this!
Since the Libra script is already a huge javascript string, modifications should be fairly easy. Let's look at the Attack line for example.
Code:
text += '\\px[100]\\c[4]ATK:\\c[0] ' + target.atk;
Most of this line is messing around with the font size and colors, which is all covered by the message plugins, so we'll leave that be.

First is figuring out how many digits we have. The simplest way in javascript is going to be .toString() to have it convert your number into text, and then just asking it how long the text is: target.atk.toString().length. If the number's negative, that length will include the - sign, for reference.

Second is quickly generating that many X's, for which we use .repeat(), which just gives you a bunch of repeats of a string- for example, "Hello".repeat(2) gives you "HelloHello". So "X".repeat(digits) should produce the number of X's we want. Since the line isn't too long yet, we can do "X".repeat(target.atk.toString().length), which converts the numerical attack stat into text form, counts how many characters that text is, and spits out an equal number of X's.

Inserted into the original code, we get this:
Code:
text += '\\px[100]\\c[4]ATK:\\c[0] ' + "X".repeat(target.atk.toString().length);

You should be able to use that same format for the rest of the stats- using mhp and mmp for health and mana, if you're just trying to show the maximum values and not the current ones.
Thanks Real Q, I appreciate your work.
Can I pm you sometimes for further info about scripting?
Just for avoiding multi thread for little things.

And as an extra info, if I would put those infos in a window-like the status window, how Can I do that? calling window function as well i think but how to configure that?
I'm not very familiar with how windows work myself, and so far my attempts at making status windows for enemies have been crashing a lot. If you can figure out how to get an enemy status window that shows the enemy's stats via text, you could still use that bit of code to replace the text with X's.

I'm only on irregularly and have limited knowledge, so making a thread with a clear outline of what you're trying to do is probably your best bet.

Also, you can find a lot of online resources for javascript, like W3 Schools.
thanks anyway 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/counting-digits-of-a-number.115293/