-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashatize.js
More file actions
38 lines (32 loc) · 810 Bytes
/
Dashatize.js
File metadata and controls
38 lines (32 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function dashatize(num) {
const splitArr = num.split("");
const newArr = splitArr.map(eachNum => {
if (eachNum % 2 === 1) {
return "-" + eachNum + "-";
} else return eachNum;
});
let newestArr = newArr.join("").split("");
const length = newestArr.length - 1;
if (newestArr[length] === "-") {
newestArr.splice(length, 1);
}
if (newestArr[0] === "-") {
newestArr.splice(0, 1);
}
return newestArr;
}
dashatize(nums);
var a = 10;
while (a < 10) {
a--;
break;
}
console.log(a);
// ----
function dashatize(num) {
const conv = (acc, val) => (val % 2 != 1) ? acc + val : acc + `-${val}-`;
const trim = (str) => str == '--' ? '-' : '';
let [...numArr] = Math.abs(num).toString();
let str = numArr.reduce(conv, '');
return str.replace(/^-|--|-$/gi, trim);
};