I think this is more like Math question rather than programming but since I’m writing Javascript, hope it is forgivable.
I receive user input, 1, 2, 3 or 4.
if 1, then output is 9
if 2, then output is 99
if 3, then output is 999
So basically input represent number of digit.
I admit that I really suck at Math. Tried to break this piece down to some kind of fomula
like 9 * 10 + 9 etc hoping I find some kind of Math approach to this problem but my brain corrupts.
I could do this by String concatenation way:
var userInput = 3;
var output = "";
for(var i=0; i<userInput; i++) {
userInput = userInput + "9";
}
console.log(userInput); //now this should have string "999"
return parseInt(userInput); //return as integer 999;
while above works, I think it is really… not cool.
Could anyone show me if I can do this using Mathematical way?
That could work. Or:
Although that’s probably less efficient.