Keeping the idea of doing with text does not need a loop:
let number = 18;
console.log(parseInt((number.toString() + '000000000').substring(0, 8)));
number = 678;
console.log(parseInt((number.toString() + '000000000').substring(0, 8)));
I put in the Github for future reference.
The formula used is to pick up the amount of digits you have.
If you prefer only to use mathematics (this is what I prefer since turning into text is a trick if you want a number, so I would discard the example by text, because it is very inefficient for the purpose):
let numero = 18;
console.log(numero * Math.pow(10, 8 - Math.log(numero) * Math.LOG10E + 1 | 0));
numero = 678;
console.log(numero * Math.pow(10, 8 - Math.log(numero) * Math.LOG10E + 1 | 0));
I put in the Github for future reference.
I did not make a check if the number of digits already 9, because this requirement was not in the original question and because it seems wrong or at least incomplete, then it would need to be better spelled out and if that’s all it is just put a if
, nothing much.
William Nascimento made a test that shows that using only mathematics is absurdly faster. I even surprised that my code manipulating string is the second fastest (muuuito ago). I thought the padEnd()
seroa faster, but it was not, this method must be very badly done, or else it is written in JS even, there would make sense he be slower. The test does not show the difference between the for
and while
as confronted in a response, but it should be the same. Of course it depends on each implementation, so test in multiple browsers. In my:
He even did the kindness of test with Node to take away the interference that every browser offers in performance, where we can see that my code string was more than the sobro of the speed of the other forms and the mathematics gave almost 100x.
I noticed that the test can still make a bigger difference. Because the question has a numerical difference, so to do with string has to make a conversion that in the test already caught done. This conversion is nothing trivial, it is not that will increase 5% the time. The effort should almost double the total required effort, at least in some cases. Imagine then having almost 200x difference.
I realize a lot of people don’t care about that. Although this case does not happen so easily, there are cases that gives a brutal difference in cost, especially if using cloud. There is a case that can make application unfeasible, to the point that people start looking for magic solutions when it gets too slow, making the application much worse because it did not care about the performance where it mattered. I’ve had several jobs that I’ve had to solve performance issues. I never did anything great, I just saw that the previous programmer was a performance buff. In general it obtained gains that varied from tens to thousands of times, and saved the application or greatly reduced the costs, in general I spent hours or days to solve it.
If you want something short throw it into a function and use it, but don’t use something unnecessary that has such a poor performance.
And if you don’t put zeros on the right, if the number has more than 9 digits, it would exceed the limit of 9?
– Guilherme Nascimento
No, it can never exceed 9 digits if the received parameter already comes with 9 digits.. nothing should happen!
– wDrik
This type of information is important, because when I saw it I thought it was the case of "PAD" (right pad), but then it is exactly. So the important thing is to detail your question from the beginning (and the future questions also) to help us help you with your problems.
– Guilherme Nascimento
Blzz will edit here to add this information!
– wDrik