0

I'm working on an algorithm where you are supposed to add 2 integers without using a + or -. So I took the two arguments, stuck them in an array, applied a forEach and incremented a counter by one as each element counted it's way down to 0. As such.

function test(a, b){

  var array  = [], counter = 0;
  array.push(a,b);
  array.forEach(function(element){
    while (element > 0) {
      counter ++;
      element --;
    }
  })

  return counter

}

This works fine. However, when I ran it through leetCode, one of it's test cases had a negative number. I erroneously assumed all cases would be positive. I rewrote the algorithm like this, to account for negatives.

function test(a, b){

  var array  = [], counter = 0;
  array.push(a,b);
  console.log(array);
  array.forEach(function(element){
    if (element > 0){    
      while (element > 0) {
        counter ++;
        element --;
      }
    } else if (element < 0) {
        while (element < 0);
          counter --;
          element ++;
    }
  })

  return counter

}

This works fine on paper, but it gets stuck in a loop when I run it in my terminal. At least I think it does. No error messages are displayed and I've consoled logged all the variables in every spot I can think off, and instead of having endlessly streaming console log numbers, which is what usually happens when I accidentally create an infinite loop, the cursor just sits there until I hit ctrl + c.

Any ideas what is causing this weird behavior? When I test it with

console.log(text(1,-1))

I would expect a return of 0,

Apparently it worked all along other then poorly placed brackets and typos. Thanks!

nwimmer123
  • 79
  • 12
  • It runs as you expected for me, I get 0. – Chris Feb 14 '17 at 22:41
  • weird. maybe it's my console. – nwimmer123 Feb 14 '17 at 22:42
  • it also times out on leetCode as written, so there must be something wrong – nwimmer123 Feb 14 '17 at 22:44
  • Test this solution on leetcode http://stackoverflow.com/questions/9070937/adding-two-numbers-without-operator-clarification it might be leetcode`s problem – Mihai Feb 14 '17 at 22:44
  • could you turn it into a snippet? Too lazy to copy paste > – A. L Feb 14 '17 at 22:44
  • _"I'm working on an algorithm where you are supposed to add 2 integers without using a + or -"_ `counter ++; element --;`? – guest271314 Feb 14 '17 at 22:48
  • 2
    yeah not sure a snippet with an infinite loop in it is a fantastic idea... – CupawnTae Feb 14 '17 at 22:51
  • You should remove the solution snippet from the question - it makes for confusing reading. See for example http://meta.stackoverflow.com/questions/319747/editing-potential-answers-into-questions – CupawnTae Feb 14 '17 at 23:08
  • 1
    sure I can remove it. I've never used one before. I just put it in for A. Lau – nwimmer123 Feb 14 '17 at 23:13
  • @nwimmer123 in general snippets are very useful, but the original one hung the browser tab, and after your edit, it didn't make sense as part of the question since it was actually the solution, and therefore confusing to someone reading the question for the first time in future. – CupawnTae Feb 14 '17 at 23:16

1 Answers1

3

You have a logic issue with your while loop:

while (element < 0);
      counter --;
      element ++;

should be

while (element < 0) {
   counter --;
   element ++;
}

After that change, your test case starts working properly.

Justin Hellreich
  • 575
  • 5
  • 15
  • yup, that's it, although now, it appears to have a problem with 2 negative numbers. Frickin typos. – nwimmer123 Feb 14 '17 at 22:52
  • Strange, `test(-1, -1)` returned `-2` for me. Does it still run without stopping? – Justin Hellreich Feb 14 '17 at 22:53
  • test(-12,-8) returns -2 as well. I'm investigating now. my gut says it's not hitting the while loop correctly then. – nwimmer123 Feb 14 '17 at 22:55
  • Are you sure you made the exact change I suggested? `test(-12, -8)` returns `-20` for me. On a side note, this is a pretty cool way to solve this problem! If you made your function accept an array it could probably work for longer arithmetic expressions. – Justin Hellreich Feb 14 '17 at 22:58
  • 1
    No, check out the latest edit it's still not fixed - the semicolon is gone but the braces haven't been added. – CupawnTae Feb 14 '17 at 22:59
  • 1
    You are still missing braces on your while loop. – Justin Hellreich Feb 14 '17 at 22:59
  • got it, I had reversed the order of counter -- and element ++ while testing and forgot to put it back. – nwimmer123 Feb 14 '17 at 23:00
  • to long sitting at the computer today. haha – nwimmer123 Feb 14 '17 at 23:00
  • apparently this is a bad solution. If you use numbers bigger then 9 digits each, then it times out. so i need to figure out a better solution. – nwimmer123 Feb 14 '17 at 23:12
  • 1
    Yup, it's definitely slow. Probably the expected solution involves bit manipulation. If that's beyond your expertise and you want to learn, start with binary representations of numbers, adding/subtracting, AND/OR, and left and right shifts. This [topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials/a-bit-of-fun-fun-with-bits/) page looks like a decent resource. – Justin Hellreich Feb 14 '17 at 23:22