Skip to content

Commit d91057a

Browse files
committed
Create Dash Insert
1 parent 8eb27c8 commit d91057a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Dash Insert

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***************************************************************************************
2+
* *
3+
* CODERBYTE BEGINNER CHALLENGE *
4+
* *
5+
* Dash Insert *
6+
* Using the JavaScript language, have the function DashInsert(str) insert dashes *
7+
* ('-') between each two odd numbers in str. For example: if str is 454793 the *
8+
* output should be 4547-9-3. Don't count zero as an odd number. *
9+
* *
10+
* SOLUTION *
11+
* I am going to loop through each number in the string. Test if number is odd using *
12+
* modulus. If odd then check if next number is also odd. I have two odd numbers then *
13+
* insert dash into the string.
14+
* *
15+
* Steps for solution *
16+
* 1) Initialize idx to zero since using this as our counter *
17+
* 2) Use While loop to loop thru string since we will be adding dashes to it *
18+
* 3) If currrent character is odd and so is next character then insert dash *
19+
* 4) return modified string as answer *
20+
* *
21+
***************************************************************************************/
22+
23+
function DashInsert(str) {
24+
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+
}
34+
}
35+
return str;
36+
37+
}

0 commit comments

Comments
 (0)