Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 1556.thousand-separator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* URL of this problem
* https://leetcode.com/problems/thousand-separator/description/
*
* @param {number} n
* @return {string}
*/

var thousandSeparator = function (n) {
let str = n.toString();
const SeparatatedStr = [];

// Extract the last 3 digits and add them to SeparatatedStr in an order by occurrence
while (str.length > 0) {
SeparatatedStr.unshift(str.substring(str.length - 3, str.length));
str = str.substring(0, str.length - 3);
}

return SeparatatedStr.join(".");
};

module.exports = thousandSeparator;
13 changes: 13 additions & 0 deletions thousandSeparator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const thousandSeparator = require("../jest-test/1556.thousand-separator");

test("Return a string wihtout any separator dot", () => {
expect(thousandSeparator(987)).toBe("987");
});

test("Return a string seperated by dots", () => {
expect(thousandSeparator(1234)).toBe("1.234");
});

test("Return a string of 0s seperated by dots if the input n consists only of 0", () => {
expect(thousandSeparator(0000000)).toBe("0.000.000");
});