forked from dalmia007/Nobody-Cares-What-The-Repo-Is-Called
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
18 lines (15 loc) · 534 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ### Challenge
// Figure out the pattern used to add the dash ('-') to the numbers by looking at the test and write a function for it.
// IMPORTANT: The resulting string cannot begin or end with a dash ('-').
// GOOD LUCK!👍
// ### Examples
// dash(274) // returns "2-7-4"
// dash(5311) //returns "5-3-1-1"
// Refer to Test for More Examples
function dash(num) {
if (isNaN(num)) return 'NaN';
else {
return String(num).replace(/[13579]/g,'-$&-').replace(/--/g, '-').replace(/^-|-$/g,'');
}
};
module.exports = dash;