Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Can anyone explain Dash Insert challenge in more detail
    I am trying to understand this problem, but by looking test cases, I can't understand what the problem is exactly asking for... Can anyone please help explain this problem? Dash Insert Challenge Thanks
    hdesai posted this question on 10/4/14 | javascript, dash, insert
    Answers
  • +
  • 2
  • -
  • Hello hdesai, The challenge requires that you insert a dash between two CONSECUTIVE odd numbers So in other words if the input number is 8865321398, then you should output the result: 8865-321-3-98. If there are no two consecutive odd numbers then no dashes would be made, e.g 123456789 then result would still be 123456789. Note: We assume that the sequence of numbers are all single digits from 0-9. 0 should be taken as an even number (not odd)/ So basically, you want to loop through the numbers and if digit[i] and digit[i+1] are both odd, then go ahead and insert a dash between digit[i] and digit[i+1]. Otherwise continue the loop without changing anything. Hint: You'll most likely be using .split, .splice and .join array methods to complete this challenge, Hope that helps!
  • +
  • 1
  • -
  • Used this exercise to practice recursion... besides it being unnecessary/more difficult to follow, is there any reason this is a bad idea?
    function DashInsert(str) { 
      let firstNumIndex = str.search(/[13579][13579]/);
      if(firstNumIndex === -1) return str;
      let firstNum = str[firstNumIndex];
      let secondNum = str[firstNumIndex +1];
      str = str.replace(/[13579][13579]/, firstNum + '-' + secondNum);
      return DashInsert(str);
    }
    DashInsert(readline());
  • +
  • 0
  • -
  • I got it...
  • +
  • 0
  • -
  • Why are you not putting a dash between the first two nines?
  • +
  • 0
  • -
  • Hi Kim, I understand that logic but I couldn't understand this test case : Input = 99946 Output = "9-9-946". According to challenge, it should o/p: 99-946 but how it's giving 9-9-946? Thanks.
  • +
  • 0
  • -
  • As mattlarsh pointed out, the first two 9's are consecutive odd numbers and therefore a dash is required between them.
    Log in to write an answer.