2

I've been having a problem that when my auto clicker in my clicker game goes fast enough to get to 200 thousand, it starts to lag, and then it doesn't function properly, or as fast.
Is there a way to make 100 thousand turn into 100K, and 101 thousand turn into 101K without being repetitive?

I tried this with my original code, and realized putting up to 1000 suffixes into each function would be a little too hard:

if (number >= 100000) {
    document.getElementById(ID).innerHTML = "100K"
} 
if (number >= 101000) {
    document.getElementById(ID).innerHTML = "101K"
} 

and on and on. I don't want multiple if statements!

This would work, but it would take up way too much space, and I know there is an easier way to it, but I just couldn't find it. Can anyone provide a way to do this?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Thomas Tallman
  • 71
  • 1
  • 1
  • 13
  • What do you mean "take up way too much space"? Could you provide a working example of what you're doing? – chazsolo Mar 29 '19 at 16:49
  • Welcome to [SO]. Please read [ask] and [mcve]. Without a proper example of the problem you are facing, we cannot help you. – Ahmed Masud Mar 29 '19 at 16:52
  • Ill try to explain: so i have a loop that has an interval of 500 milliseconds. when it gets clicked multiple times it can go very fast and reach the hundreds of thousands. i want to know how to make it so that when the number reaches 100,000 it changes to 100K and keeps looping. and by take up way too much space, i mean having to make an if statement for every thousand, and that creates too many if statements within the functions. – Thomas Tallman Mar 29 '19 at 16:55
  • So what exactly does the original string look like? `'100,000'` or `'100 thousand'`? Can you not pull digits off the string, divide the number by `1000`, and if it is >=100, replace the `,000` or `thousand` portion of the string with `K` ? Or, where ever the string originally gets created, put your if-then statement there? Either way, you could create a function for determining the format of the output string (depending on how large the number is). You definitely should *not* have a contingency if-then customized for every possible number! – SherylHohman Mar 29 '19 at 18:04
  • it isn't a string at all, it is actually a number. i used a for loop and assign the variable number 0, then inside of the for loop put a document.getelementbyId and at the end did number += 1. the div with the id number would slowly increase. i want to turn a number into a string. maybe that is my problem? – Thomas Tallman Mar 29 '19 at 18:07

1 Answers1

4

Try separating the job of formatting your number into a different function.

SUFFIXES = 'KMBTqQsSOND' // or whatever you'd like them to be
function getSuffixedNumber(num) {
    var power = Math.floor(Math.log10(num));
    var index = Math.floor(power / 3);
    num = Math.round(num / Math.pow(10, (index * 3))); // first 3 digits of the number
    return num + (SUFFIXES[index - 1] || ''); // default to no suffix if we get an out of bounds index
}

You can call the function like this: var x = getSuffixedNumber(101000), the value of x will be "101K".

Elizabeth
  • 499
  • 2
  • 10
  • 2
    Ooh… this is clever. Nicely done. Maybe add something like `index = Math.min(index, SUFFIXES.length)` to avoid falling off the end of the list? –  Mar 29 '19 at 17:36
  • This is also very close to what i want, i just want a clear example of a intervalled loop with this. – Thomas Tallman Mar 29 '19 at 17:38
  • for now, i think this is the closest answer, and since the other was deleted the only answer, so im accepting it. But i would like to receive other answers if possible. – Thomas Tallman Mar 29 '19 at 18:25
  • what do you put for (num)? how exactly do i use this in an if statement? – Thomas Tallman Apr 02 '19 at 15:19
  • 1
    Hey Thomas, 'num' is whatever you put in the parentheses when you call this function. `getSuffixedNumber(250000000)` is the value `"250M"`. – Elizabeth Apr 02 '19 at 16:46
  • so, if i called the function and put my variable number in it, then that would work? would i need to call that inside of every function? if so that's fine because its much easier. i use `document.getElementById` as shown in my question, so would i need to use that? if so can you provide an example, but if i don't then i think my question has been appropriately answered. – Thomas Tallman Apr 02 '19 at 16:58
  • one last question, what about all of the other parenthesis? would i put my variable there too? – Thomas Tallman Apr 02 '19 at 17:02
  • 1
    Hey Thomas, check out this example: https://codepen.io/Dibasic/pen/OGVrQd all you need to do is take out the `COUNT *= 2` and handle increasing your count somewhere else. – Elizabeth Apr 02 '19 at 17:16
  • my school chromebook blocked it. ill check it out later. im guessing `COUNT *= 2` is in the example, so i will check that out as soon as possible. – Thomas Tallman Apr 02 '19 at 17:21
  • I checked it out, and i got it to work the way i wanted to to. i think i can incorperate this method into my code now. – Thomas Tallman Apr 03 '19 at 11:57
  • Ethan can you create a chat room and add me to it? – Thomas Tallman Apr 03 '19 at 12:22
  • Ethan, i have a new question. I want to add something like 1.2M or 1.2k. can you edit your formatting function to support that? if i press on one of the buttons that reduces the variable number, instead of going from 10k to 9.5k, or even 9k, it stays at 10k. that's why i need the decimals. – Thomas Tallman Apr 03 '19 at 15:16