View Full Version : Customizing prompt info through methods HELP!

09-29-2011, 06:45 AM
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.


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

<script type="text/javascript">

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 type="text/javascript">

info ( );

teacher = new Person();


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

else{tempString = teacher.area +''+ teacher.num}

document.writeln ( tempString);


Old Pedant
09-29-2011, 09:42 PM
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:

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? ...

09-29-2011, 11:15 PM
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?

Old Pedant
09-30-2011, 04:23 AM
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.

Old Pedant
09-30-2011, 04:25 AM
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.

09-30-2011, 11:31 PM
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 ?