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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Customizing prompt info through methods HELP!

    Hello all.

    I have a script to prompt a user for 4 pieces of information.

    First name.
    Last name
    Area code.
    & Telephone number.

    My assignment was to do this, and put the information is as follows.

    If both, first name & last name are entered, store in object as

    last name,first name.

    If only one is entered. display without the comma.

    Also store telephone number in (XXX) XXX-XXXX format.

    I was told to use methods to do all of these things.

    Please take note I am in a VERY basic javascript class.

    Thanks in advance.

    CODE I HAVE SO FAR :


    head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>


    <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-29-2011 at 06:12 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    I don't see any attempt to CONDITIONALLY put a comma after the last name if there is a first name.

    I don't see any attempt to format the phone number as specified.

    Also, you missed a vital point:
    If both, first name & last name are entered, store in object as
    last name,first name.
    You don't haven any "object".

    You need to create an object. Various ways to do that. I'd keep it simple:
    Code:
    var Person = new Object();
    Person.name = last; 
    ... now what do you put here that will add on a comma and first name if first name was entered? ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Person.name = last, first; ??

    or would you use an if/else statement?

    I'm aware I didn't attempt to. I'm not looking for just an answer, I just didn't know how to go about starting it.

    I thought the new Person ( ); was the creation of the new object, I guess not?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Yes, teacher = new Person() creates an object, but (a) you never then assign and values to the properties of teacher. (b) and your Person constructor is invalid. It asks for one argument (info) but then tries to assign values to four different properties.

    And, again, as I read the assignment, the Person object should *NOT* have both a first and last name property. It should have just a name property and the value of the property has to follow the rules about last, first or just last.

    And we haven't started on formatting the phone number, yet.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    In other words, I think you got the cart before the horse.

    Worry about creating the name field in the form asked for.
    Worry about creating the phone field formatted as asked for.

    Don't worry about trying to build a constructor. You say this is a very basic class, so I'm surprised to see you trying to use a constructor.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So would I need to go back and change the name properties? such as :

    this.name = (first, last)

    And if I wanted to call it, would it be name.first & name.last ?


  •  

    Posting Permissions

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