0

Looking around Stackoverflow, I have came across this JSFiddle, however this script appears to have errors inside, of which due to my experience I do not fully understand how to repair.

Running through JSBeautifier, all of the code is nicely indented apart from the following having no indents;

$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 0, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method for when the element finishes updating
};
})(jQuery);

In addition to this, I wish to add commas to my number like so:

Number(10000).toLocaleString('en');  // "10,000"

1). How to fix the code a person answered on Stackoverflow, got rated up and marked as the answer regardless of the errors found?

2). How do I add commas to the output?

Tim Marshall
  • 360
  • 5
  • 25
  • 2
    I see no errors in the script you linked to. And what have you tried to get the commas inserted? – j08691 Feb 25 '15 at 02:51
  • Code to add commas is here: http://stackoverflow.com/questions/1068284/format-numbers-in-javascript/7125034#7125034 – jfriend00 Feb 25 '15 at 02:54
  • @j08691 Directly copping this code into Dreamweaver it flags line 45. As to what I've tried, I've tried multiple places such as `value = Number(options.to).toLocaleString('en');` – Tim Marshall Feb 25 '15 at 02:55
  • @jfriend00 I'm not asking how to comma a number, yet how do I comma the output number in the provided script as I have posted a working example on how to comma a number. – Tim Marshall Feb 25 '15 at 02:57
  • change `value.toFixed(options.decimals)` to `$(_this).html(value.toLocaleString());` – j08691 Feb 25 '15 at 02:58
  • Tim, you take this function and call it on the counter value right before it is inserted into the DOM. That's how you take a counter value and get a version of it with commas in it. You insert this in one line in your script. Are you having difficulty making a one line modification to the script? – jfriend00 Feb 25 '15 at 02:58
  • Tim, here's [a version of the jsFiddle](http://jsfiddle.net/jfriend00/6sgh2941/) with the `addCommas()` code added. Unsure what you're asking for. – jfriend00 Feb 25 '15 at 03:00
  • Link added to my previous comment. You need to understand each line of the code you have and it should be obvious where the value is being inserted into the DOM for display. That's where you want to put the commas in. – jfriend00 Feb 25 '15 at 03:03
  • I've been looking all over but could not find the location. Suffering bad with my depression and PTSD right now so not in the right grame of mind but trying to fight it! Add your comment as an answer so I can accept – Tim Marshall Feb 25 '15 at 03:05

2 Answers2

1

Here's a version of the jsFiddle with the addCommas() code inserted.

It was inserted into this line:

$(_this).html(addCommas(value.toFixed(options.decimals)));

If you want to use this to add commas:

Number(10000).toLocaleString('en');  // "10,000"

then, please make sure it is supported in all browser versions you want your app to run in. I can't tell for sure, but it might require IE 11. I tried your method in IE9 and IE10 and it seems to add two decimal places when they are not asked for. IE11 works as expected.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Just add .replace(/\B(?=(\d{3})+(?!\d))/g, ",") to value.toFixed(options.decimals) on line 19: JSFiddle

Boxer
  • 1