Let’s Chunk – Part Two

See the original posting on DZone Python

In my previous article, we talked about the chunk function and implemented it in JavaScript. Today I’ll try to show an example for using this function. This example popped into my mind when I read about the chunk function, because it reminded me of a question I once gave to my students in an exam, and I think the chunk function could have been helpful for them had they known it.

The task goes like that – write a function called printWithCommas. The function would get two integers – num and d. The function would return a string that represents num with a comma after every d digits from the right. It is possible that the last leftmost part wouldn’t contain exactly d digits, according to d and the total digits in the number.