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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Function & Object help.

    EDIT*****

    I'm nearly done with my script. I just lack one thing!


    I have 4 prompts asking a user for information.

    Basically all I want to do is..

    If lastname, and first name are BOTH entered, seperate them with a comment like so : lastname,firstname

    but leave the comma out, if only one of the names are entered.

    How could I do this?




    <script type="text/javascript">
    last=name=area=num='';

    function info ( ) {
    last = prompt("Please enter your last name",'');
    name = prompt("Please enter your first name",'');
    area = prompt("Please enter your area code",'');
    num = prompt("Please enter your phone number",'');
    };

    function Person (info) {
    this.name = name
    this.last = last
    this.area = area
    this.num = num
    }

    </script>

    <title></title>
    </head>

    <body>

    <script type="text/javascript">

    info ( );
    teacher = new Person();

    tempString='';

    document.writeln ( last );
    document.writeln ( name) ;


    if(area==''||num==''){tempString='';}
    else{tempString = teacher.area +''+ teacher.num}


    document.writeln ( tempString);




    </script>
    Last edited by adamdressler; 09-27-2011 at 10:45 AM.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Your vars in info() are local to that function and not accessible outside of that function. If you left out the "var " they would be global and accessible in any function. Iis this what you need? or do you need them to be local and "passed" to the Person() function?

    also:
    Code:
    this.name = var name
    is wrong try:
    Code:
    this.name = name

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I didn't think those var's belonged there. Yes, I need it to be global. Basically here's what I need to do.

    Prompt user, get info. Store info in Person object, and then display it using method.

    And then a couple of conditions, such as displaying the first and last name if one is left blank, but NOT displaying the phone number if the area code is left blank.


    ***Or maybe they need to be passed? Once they are figured out by the prompts? The global thing confuses me.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I'm not sure on the syntax, but I believe you have created an object constructor, and not just an object. This means that in order to set the values of its properties you would want to create an instance of the object, I don't think that it is even evaluated until that happens (based on my past experiences with uncalled functions not generating an error, but I have nothing further to support that theory...).

    Code:
    Teacher = new Person();

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm aware of the constuctor function. I figured i would need this whenever I set the criteria of not showing stuff if it wasn't entered.


    This code runs the prompts, and displays the entrys. Now I just need to figure out how to NOT show the telephone number if there is NO AREA CODE ENTERED.

    and Display the firstname/last name, even if one or the other isn't entered..





    <script type="text/javascript">
    function info ( ) {
    name = prompt("Please enter your first name");
    last = prompt("Please enter your last name");
    area = prompt("Please enter your area code");
    num = prompt("Please enter your phone number");
    };

    function Person ( ) {
    this.name = name
    this.last = last
    this.area = area
    this.num = num
    }

    </script>

    <title></title>
    </head>

    <body>

    <script type="text/javascript">

    info ( );

    document.writeln ( name) ;
    document.writeln ( last);
    document.writeln (area);
    document.writeln ( num);


    </script>

    </body>
    </html>

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Global is just scope: when you put a variable inside of a function and call it local, you can only read it or write to it inside of that function:
    Code:
    function MyFunction(){
     var MyInteger = 5;   // this is local
     document.write(MyInteger) // this will work, because it is inside the function
     }
     document.write(MyInteger) // this won't work, because it is outside the function
    But if it is global, you can read it, and write to it anywhere in the document:
    Code:
    function MyFunction(){
     MyInteger = 5;   // this is globla
     document.write(MyInteger) // this will work, because  globals can be accessed inside the function
     }
     document.write(MyInteger) // this will work, because  globals can be accessed outside the function

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    That would be the flow of logic... My favorite part!

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    For each value you want to test for your criteria, and if it is true, allow the write, if it is false, don't allow the write

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    So, how do you test the first criteria?

  • #10
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Would you create a whole new function for this? or would I attach it to the already existing constructor function?

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Not so fast, if you already ran the info() function, then it should return to the next line after it executes right?

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    you could just as easily allow the execution to continue outside of a function if you please. Is that allowed by your criteria?

  • #13
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, it continues to the next line. All of my displays seem to work properly. However here it the only problem I need fixed.

    If last name and first name are BOTH entered. Text should be displayed as : lastname,firstname.

    But if only one is entered. Display it without a comma.

    And. If area code OR number is not entered, then don't display any number at all.

  • #14
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    consider this:
    Code:
    <script type="text/javascript">
    info ( );
    document.writeln ( name) ;
    document.writeln ( last);
    document.writeln (area);
    // statement to test whether the area code was entered
    document.writeln ( num);
    
    
    </script>


    Edit:
    What would that statement be?

  • #15
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I believe I have all of that code correct.

    I'm not sure on how to add criteria to my inputs.

    Does it have something to do with info.name || ""; ?

    because i can't figure out how to add the comma, and take out the comma if data was not entered.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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