www.webdeveloper.com
Results 1 to 10 of 10

Thread: javascript code behavior changes when I attempt to debug it

  1. #1
    Join Date
    Jul 2013
    Posts
    4

    javascript code behavior changes when I attempt to debug it

    I have some javascript code that uses a `for` loop to construct a string. When the code runs, it appears that the number of loop iterations fewer than expected. If I attempt to debug the code by adding `window.alert` statements, the behavior changes and I get the correct number of loop iterations. Has anyone else encountered something like this?

  2. #2
    Join Date
    Dec 2012
    Posts
    168
    Try debugging by outputting a message to the console instead of using alerts

    Code:
    console.log('message');

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by pfeldman View Post
    I have some javascript code that uses a `for` loop to construct a string. When the code runs, it appears that the number of loop iterations fewer than expected.
    How are you determining that?

  4. #4
    Join Date
    Jul 2013
    Posts
    4
    I'm assembling the string as follows (my apology for not using the write markup; I can't find the instructions):

    for (var k=3; k<template.length; k++) {
    hint+= template[k] + '\n';
    }

    // Remove final newline character:
    hint= hint.substring(0, hint.length-1);

    Without the debug statements, the resulting string is missing some of the items that it should have included. When I add the window.alert statements, I get the correct result. It's been about a decade since I last encountered a situation where debugging changed the behavior of the code.

    Phillip

  5. #5
    Join Date
    Mar 2011
    Posts
    1,158
    Try:
    Code:
    hint = k.join("\n").substring(6);

  6. #6
    Join Date
    Mar 2009
    Posts
    521
    Are you changing the template array-- adding or subtracting elements to it? This can cause really ugly things to happen in a for loop which depends upon the length of the array.

  7. #7
    Join Date
    Jul 2013
    Posts
    4
    Why the 6?

  8. #8
    Join Date
    Jul 2013
    Posts
    4
    The template array does not change.

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by pfeldman View Post

    Without the debug statements, the resulting string is missing some of the items that it should have included.
    Can you provide a full runnable example that demonstrates your perceived problem?

  10. #10
    Join Date
    Mar 2011
    Posts
    1,158
    Ignore my code above. I got sloppy. It doesn't do what you wanted and it doesn't even do what I intended.

    Try:
    Code:
    hint = template.slice(3).join("\n");
    This code lops off the first three elements of 'template', and glues the remaining elements together with a "/n". If that's what you need, it should work.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles