11

running the following code in nodejs cli:

var my_function = function() {                                                                                              
    var next_value = 1
      , value = undefined
      , difference = undefined                                                                                              
      , prev_difference = undefined                                                                                         

    while ((typeof prev_difference === 'undefined') || (prev_difference > 0)) {                               
        value = next_value 
        next_value = 2
        difference = next_value - value 
        if (difference > prev_difference) {                                                                                 
              throw new Error('Diminishing')
        }                                                                                                                   
        prev_difference = 0 
    }                                                                                                                       
    return next_value 
}              

for (var i = 0; i< 300; i++) { console.log(i); console.log(my_function()) }

At iteration 282 of the loop I start getting the value '1' instead of '2'. Can't for the life of me understand why. This chunk of code is a reduction from something else I've been working on, hence the seemingly unnecessary if statement within the loop. There are a few ways to change this code such that the execution path does not get screwed up, but I'd like to understand why it's breaking with the current setup.

Also, if you have any tips for tools that could aid me in debugging something like this in the future I would be very grateful. For the most part I used console.log to narrow this down.

node.js version v0.8.6. Running on Mac OSX version 10.7.5. Thanks

13
  • This seems like a Node bug to me; the behavior is really strange. (I can duplicated it too.) edit I guess it may be a V8 bug and not a Node bug ... Commented Nov 4, 2012 at 3:42
  • Don't think it's V8 because Chrome doesn't seem to have the same problem... ? Commented Nov 4, 2012 at 3:55
  • I'm running Node 0.8.6 on Linux (32-bit), and I don't show that behavior. Commented Nov 4, 2012 at 4:04
  • Node v0.8.14 (Ubuntu 64bit) also see this bug. I think it need to be reported here github.com/joyent/node/… Commented Nov 4, 2012 at 4:07
  • @VadimBaryshev: Do you see the bug in Chrome? Commented Nov 4, 2012 at 4:08

1 Answer 1

2

If you take out the IF statement it is gonna be fine, it will return only '2' on every iteration. The variable prev_difference is 'undefined' every time and this seems to cause issues.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, we have been able to figure that out. The question is why? Because it shouldn't. It's perfectly valid to work with undefined variables in JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.