File tree Expand file tree Collapse file tree 1 file changed +17
-12
lines changed
Expand file tree Collapse file tree 1 file changed +17
-12
lines changed Original file line number Diff line number Diff line change 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"
You can’t perform that action at this time.
0 commit comments