-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDashInsertII.js
63 lines (45 loc) · 1.41 KB
/
DashInsertII.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Using the JavaScript language, have the function DashInsertII(num)
insert dashes ('-') between each two odd numbers and insert asterisks
('*') between each two even numbers in num. For example: if num is
4546793 the output should be 454*67-9-3. Don't count zero as a negative
or positive number.
*/
function DashInsertII(num) {
var numStr = String(num);
var numArr = numStr.split('');
var checkedArr = [];
// console.log(numArr);
if (numArr.length > 1) {
for (i = 0; i < numArr.length - 1; i++){
var checker = numArr[i];
var nextChecker = numArr[i+1];
if (checker % 2 == 0 && nextChecker % 2 == 0 && checker != 0 && nextChecker != 0 && nextChecker != undefined){
checkedArr.push(checker + '*');
//break;
} else if (checker % 2 != 0 && nextChecker % 2 != 0 && checker != 0 && nextChecker != 0 && nextChecker != undefined){
checkedArr.push(checker+'-');
} else {
checkedArr.push(checker);
}
// if (nextChecker != undefined){
// checkedArr.push(nextChecker);
// }
}
checkedArr.push(numArr[numArr.length - 1]);
} else {
checkedArr.push(numArr[0]);
}
var checkedStr = checkedArr.join('');
return checkedStr;
}
//DashInsertII(99946);
//DashInsertII(56647304);
//DashInsertII(56730);
//DashInsertII(567);
//DashInsertII(779933);
//DashInsertII(667488958374553);
//DashInsertII(60497);
//DashInsertII(399047);
DashInsertII(2129);
//DashInsertII(77993);