Skip to content

Commit da731ce

Browse files
authored
Update Dash Insert
1 parent 1b8320a commit da731ce

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Dash Insert

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@
2020
* *
2121
***************************************************************************************/
2222

23-
function DashInsert(str) {
23+
function DashInsert(str) {
24+
let result = '';
2425

25-
var idx = 0;
26-
while (idx < str.length-1) {
27-
if (Number(str[idx]) % 2 === 1 && Number(str[idx+1]) % 2 === 1) {
28-
str = str.slice(0,idx+1) + "-" + str.slice(idx+1);
29-
idx = idx + 2;
30-
}
31-
else {
32-
idx++;
33-
}
26+
for (let i = 0; i < str.length; i++) {
27+
result += str[i];
28+
if (i < str.length - 1) {
29+
const currentNum = parseInt(str[i]);
30+
const nextNum = parseInt(str[i + 1]);
31+
if (currentNum % 2 !== 0 && nextNum % 2 !== 0) {
32+
result += '-';
33+
}
34+
}
3435
}
35-
return str;
36-
36+
37+
return result;
3738
}
39+
40+
// Test cases
41+
console.log(DashInsert("454793")); // Expected output: "4547-9-3"
42+
console.log(DashInsert("123456")); // Expected output: "123456"

0 commit comments

Comments
 (0)