Loading ...
Sorry, an error occurred while loading the content.

Possible error: Bad Type

Expand Messages
  • sandyhead25
    var a, b = [], c = [ w , e ]; for (a = 0; a
    Message 1 of 4 , Jun 13, 2011
    • 0 Attachment
      var a, b = [], c = ["w", "e"];
      for (a = 0; a < b.length; a += 1) {
      for (c = a + 1; c < b.length; c += 1) {
      if (b[c] === b[a]) {
      return a;
      }
      }
      }

      The code produces this error:
      Problem at line 4 character 16: Bad type.

      It appears that this algorithm is having difficulty with "Tolerate type inconsistency" option enabled. You can put anything you want into array "b" and even make it identical to array "c" and the error will always occur. All that matters is that "c" begins its life as an array but is transmuted into a number during the expression of the second "for".
    • Dominic Mitchell
      ... Should you really be using the same variable to hold both an array and a number? That s what JSLint is warning about. If you don t like it, turn off the
      Message 2 of 4 , Jun 14, 2011
      • 0 Attachment
        On Mon, Jun 13, 2011 at 11:42 PM, sandyhead25 <austin.cheney@...>wrote:

        > var a, b = [], c = ["w", "e"];
        > for (a = 0; a < b.length; a += 1) {
        > for (c = a + 1; c < b.length; c += 1) {
        > if (b[c] === b[a]) {
        > return a;
        > }
        > }
        > }
        >
        > The code produces this error:
        > Problem at line 4 character 16: Bad type.
        >
        > It appears that this algorithm is having difficulty with "Tolerate type
        > inconsistency" option enabled. You can put anything you want into array "b"
        > and even make it identical to array "c" and the error will always occur.
        > All that matters is that "c" begins its life as an array but is transmuted
        > into a number during the expression of the second "for".
        >

        Should you really be using the same variable to hold both an array and a
        number? That's what JSLint is warning about. If you don't like it, turn
        off the type option. But you may be better off introducing another variable
        to hold the index, instead of reusing c.

        -Dom


        [Non-text portions of this message have been removed]
      • mathew
        ... It s called dynamic programming? I use JavaScript variables to hold values parsed from JSON, and then examine them to see if they re arrays or single
        Message 3 of 4 , Jun 14, 2011
        • 0 Attachment
          On Tue, Jun 14, 2011 at 04:15, Dominic Mitchell <dom@...>wrote:

          > Should you really be using the same variable to hold both an array and a
          > number?
          >

          It's called dynamic programming?

          I use JavaScript variables to hold values parsed from JSON, and then examine
          them to see if they're arrays or single numbers. If these JSlint changes are
          going to clash with that common use case, I see a problem.


          mathew
          [ Can't tell if they do as I haven't updated in a while. ]


          [Non-text portions of this message have been removed]
        • Douglas Crockford
          ... Thanks. Please try it now.
          Message 4 of 4 , Jun 14, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@...> wrote:
            >
            > var a, b = [], c = ["w", "e"];
            > for (a = 0; a < b.length; a += 1) {
            > for (c = a + 1; c < b.length; c += 1) {
            > if (b[c] === b[a]) {
            > return a;
            > }
            > }
            > }
            >
            > The code produces this error:
            > Problem at line 4 character 16: Bad type.

            Thanks. Please try it now.
          Your message has been successfully submitted and would be delivered to recipients shortly.