...

View Full Version : Function & Object help.



adamdressler
09-27-2011, 07:41 AM
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>

blaze4218
09-27-2011, 07:49 AM
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:

this.name = var name
is wrong try:

this.name = name

adamdressler
09-27-2011, 07:51 AM
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.

blaze4218
09-27-2011, 07:55 AM
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...).


Teacher = new Person();

adamdressler
09-27-2011, 07:58 AM
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>

blaze4218
09-27-2011, 08:02 AM
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:

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:

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

blaze4218
09-27-2011, 08:03 AM
That would be the flow of logic... My favorite part!

blaze4218
09-27-2011, 08:04 AM
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

blaze4218
09-27-2011, 08:06 AM
So, how do you test the first criteria?

adamdressler
09-27-2011, 08:06 AM
Would you create a whole new function for this? or would I attach it to the already existing constructor function?

blaze4218
09-27-2011, 08:08 AM
Not so fast, if you already ran the info() function, then it should return to the next line after it executes right?

blaze4218
09-27-2011, 08:10 AM
you could just as easily allow the execution to continue outside of a function if you please. Is that allowed by your criteria?

adamdressler
09-27-2011, 08:10 AM
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.

blaze4218
09-27-2011, 08:12 AM
consider this:


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

adamdressler
09-27-2011, 08:15 AM
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.

blaze4218
09-27-2011, 08:17 AM
Ok, I'm having a hard time keeping up with what your figuring out and when... I'm trying to go one step at at time, so now you've lost me :(
could you show me the "correct code" so I could catch up?

adamdressler
09-27-2011, 08:19 AM
</script>
[/CODE]

Edit:
What would that statement be?[/QUOTE]



Something boolean I'm assuming?

I'm not sure how to exactly type it.

area ? info.area || ""; SOMETHING LIKE THAT ?

adamdressler
09-27-2011, 08:20 AM
I'm sorry. Instant message would be easier, but forums aren't instant.

here is my latest code! :

<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 = info.name
this.last = info.last
this.area = info.area
this.num = info.num
}

</script>

<title></title>
</head>

<body>

<script type="text/javascript">

info ( );

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




</script>

blaze4218
09-27-2011, 08:22 AM
totally!

adamdressler
09-27-2011, 08:23 AM
What would be the exact line of code? because I add what I thought it was and nothing will run now.

blaze4218
09-27-2011, 08:24 AM
what did you try?

adamdressler
09-27-2011, 08:25 AM
document.writeln ( name) ;
document.writeln ( last);
document.writeln (area);
area ? info.area || "";
document.writeln ( num);
num ? info.num || "";

blaze4218
09-27-2011, 08:28 AM
Hey, you wanna try a chatroom? this is killing me! http://www.asap.org/chat/index.php
I'm not sure if that works, i just googled it (i don't have IM)

adamdressler
09-27-2011, 08:29 AM
do you have facebook? they have a messenger. if not. ill try this classroom.

blaze4218
09-27-2011, 08:29 AM
no facebook, I'm lame

adamdressler
09-27-2011, 08:40 AM
document.writeln ( name) ;
document.writeln ( last);
document.writeln (area);
tempString = !area?'':info.area;
document.writeln ( num);


I already have it to where it won't display anything if AREA is not entered (obviously because there is no data).

what i need is to Not display AREA OR NUM, if either is not entered.

adamdressler
09-27-2011, 08:41 AM
<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 = info.name
this.last = info.last
this.area = info.area
this.num = info.num
}

</script>

<title></title>
</head>

<body>

<script type="text/javascript">

info ( );

document.writeln ( name) ;
document.writeln ( last);
document.writeln (area);
tempString = !area?'':info.area;
document.writeln ( num);




</script>

blaze4218
09-27-2011, 09:01 AM
<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 = info.name
this.last = info.last
this.area = info.area
this.num = info.num
}

</script>

<title></title>
</head>

<body>

<script type="text/javascript">

info ( );

document.writeln ( name) ;
document.writeln ( last);
document.writeln (area);
tempString = !area?'':info.area;
document.writeln ( tempString);




</script>

blaze4218
09-27-2011, 09:13 AM
<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 ( name) ;
document.writeln ( last);

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


document.writeln ( tempString);




</script>

Dormilich
09-28-2011, 07:07 AM
Id solve it by a function.

// constructor
function Person(first, last)
{
this.firstname = "";
this.lastname = "";
if (typeof first == "string") {
this.firstname = first;
}
if (typeof last == "string") {
this.lastname = last;
}
}
// add a function to the object
Person.prototype.showName = function()
{
if (this.firstname && this.lastname) {
return this.lastname + ", " + this.firstname;
}
return this.lastname + this.firstname;
};

var x = new Person("John", "Doe");
alert(x.showName());
var y = new Person("Jane", 5);
alert(y.showName());

blaze4218
09-28-2011, 02:45 PM
@Dormilich of course, but his question is a homework assignment where he is learning the basics of conditional logic... I think you just took him 3 chapters ahead :) also, he needs to retrieve the data from a prompt... We were working the problem line by line, (that's why we left the forum)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum