Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    js interpreter ruins my day

    hi, after searching many hours for a solution i dont find a solution for my problem:
    Code:
    /*1*/	function fField(Value){
    /*2*/		this.Value = Value;
    /*3*/	}
    /*4*/	aPlayGround = new Array();
    /*5*/	aPlayGround[0] = new fField(0);
    /*6*/
    /*7*/	var Actual = 0;
    /*8*/	var Width = 1;
    /*9*/	var bBool = new Boolean(Actual%Width); //left becomes 0%1 ----> false
    /*10*/	
    /*11*/if (bBool && (aPlayGround[Actual-1].Value < 9)) {}
    if i let my browser execute this codeblock i get this error message via mozilla firebug:
    "TypeError: aPlayGround[(Actual - 1)] is undefined" in line 11
    bBool is my error handler to make sure that the second condition in line 11 doesnt get checked but he still does
    it works if i change line 11 to:
    Code:
    /*11*/if (false && (aPlayGround[Actual-1].Value < 9)) {}
    but thats obviously no solution for me

    my soul for some help...maybe a workaround for ignoring the undefined error?
    maybe another safe check for undefined values in my aPlayground[-1]?

    -dools

  • #2
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got a solution from another source:

    Code:
    var bBool = new Boolean(Actual%Width);
    is the problem
    solution:
    Code:
    var bBool = !!(Actual%Width);
    thank you, unknown user

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    When you create a new Boolean() you are creating an object. All comparisons that involve an object evaluate the object as true.

    The Boolean object serves no purpose in JavaScript except to confuse those who use it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Quote Originally Posted by felgall View Post
    The Boolean object serves no purpose in JavaScript except to confuse those who use it.
    well booleans needs a constructor, or at least a special valueOf(). you can also give it prototypes, although i can really only think of one that's useful, and even then, not all that much.

    But, Boolean does have one great use: as an array filter.
    example: get a non-sparse list of all external script URLs.

    Code:
    var sources=[].slice.call(document.getElementsByTagName("script"))
                               .map(function(a){  return a.src; })
                               .filter( Boolean ) ;
    Last edited by rnd me; 07-12-2013 at 12:47 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by rnd me View Post
    But, Boolean does have one great use: as an array filter.
    example: get a non-sparse list of all external script URLs.

    Code:
    var sources=[].slice.call(document.getElementsByTagName("script"))
                               .map(function(a){  return a.src; })
                               .filter( Boolean ) ;
    Okay there are a few advanced uses.

    Most intermediate level JavaScript users don't need it though - they should just be referencing the constant values true and false if they need to assign a boolean value (and beginners don't even need to do that).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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