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