Let’s Chunk — Part Two

function printWithCommas(num, d) {
var numArr = [];
// convert num to array
while(num != 0) {
numArr.push(num % 10);
num = Math.floor(num / 10);
}
var res = “”; // create chunks of d elements each
var chunks = chunk(numArr, d).reverse();
// concat each chunk with a comma separator
chunks.forEach(function(chunk, ind) {
chunk.reverse().forEach(x => res += x);
if(ind < chunks.length — 1)
res += “,”;
});
return res;
}

--

--

I’m an academic and freelancer instructor, teaching computer science and software engineering for more than 17 years now.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shay Tavor

Shay Tavor

20 Followers

I’m an academic and freelancer instructor, teaching computer science and software engineering for more than 17 years now.