Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2003, 11:04 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert variable to boolean

I'm new in javascript and I want to create a function to convert a variable to boolean. The variable may containt a boolean, an integer or a string. The conversion have to follow these rules:

For boolean
 
  • return the same value


For integer
 
  • 0 return false
  • all other value return true

For string
 
  • equal to 'true'; return true
  • equal to 'false'; return false
  • len greater than 0; return true
  • len equal to 0; retrun false


Any help would be apreciated.

Stéphane
__________________
Stéphane

A programmer is a device that transform coffee in code lines

\"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.\" Rich Cook
 
Old June 21st, 2003, 08:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

:D

you can do something like this:

<script language="javascript">

function getBoolean(variable) {

    var vtype;
    var toReturn;

    if(variable != null) {

        switch(typeof(variable)) {
            case 'boolean':
                vtype = "boolean";
                return variable;
                break;
            case 'number':
                vtype = "number";
                if(variable == 0)
                    toReturn = false;
                else toReturn = true;
                break;
            case 'string':
                vtype = "string";
                if(variable == "true")
                    toReturn = true;
                else if(variable == "false")
                    toReturn = false;
                else if(variable.length > 0)
                    toReturn = true;
                else if(variable.length == 0)
                    toReturn = false;
                break;

                            }
        return toReturn; }
                    }
var ok = false;
getBoolean(ok);

</script>

:D

the genuine genius
 
Old June 25th, 2003, 08:12 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, it work perfectly :)

Stéphane





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a string into a variable jtrifts VBScript 4 February 21st, 2008 09:08 AM
help me with boolean array Un33k Pro Visual Basic 2005 1 June 12th, 2007 11:43 AM
boolean starlightt Java Basics 2 April 6th, 2006 05:34 PM
Convert a variable John Anthony Access VBA 1 September 19th, 2003 10:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.