Hello,

New to JavaScript and I have an assignment where one piece of it needs to have the first name, last name, area code, and phone number when prompted by a function, once that info is had, it then needs to use the constructor function that is there to process the data and lastly use the data to output it with a show function. I have it to work with static information, but cant seem to figure what I am missing or how to make the prompt function information to feed to the constructor, I know when using the static variable it uses "new Function" but that seems to be my problem, I am stuck.

Please see the code below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab03</title>


<script type="text/javascript">
	function getinfo( ) {
		//Prompting for users information
	    var first_name = prompt( "Please enter your first name: " );
		var last_name = prompt( "Please enter your last name: " );
		var area_code = prompt( "Please enter your area code: " );
		var phone_number = prompt( "Please enter your phone number: " );
		}



	function show_person( ) {
		//Displays the infomation
		document.writeln( "<hr/>" );
		document.writeln( "Name:  " + this.last_name + ", "  );
		document.writeln( this.first_name + "<br/>" );
		document.writeln( "Phone number: " + "(" + this.area_code + ") " + this.phone_number + "<br/>" );
		document.writeln( "<hr/>" );

	}


	function Person( persons ) {
                // Constructor function
		this.first_name =  persons.first_name || "";
		this.last_name =  persons.last_name || "";
		this.area_code =  persons.area_code || "";
		this.phone_number =  persons.phone_number || "";

		this.show = show_person;
	}
</script>



</head>

<body>
	<script type="text/javascript">
	
		getinfo();
		//getinfo.show();
		
		
		
		//testing with
		var person1 = new Person( {first_name: "James", last_name: "Johnson", area_code: "918", phone_number: "688-3953"} );
		                        
		
		person1.show();

		
		
		

	</script>

</body>
</html>

off subject: "static" has a specific meaning, from Java, that you want to avoid. "Fixed" would be better.

Your prompting function collects four vars with the data you want. How about using those facts to create and return a new Person?

function getinfo() {
// get the info
var person = new Person ( first_name, etc. );
return person;
}

By the by, where are you studying this? Teaching OO JavaScript is very right, but extremely rare!

Martin

I am studying it at Tulsa Community College.

So you are talking about additionally adding that to my function or replacing all my vars with that one like of code?

ie.

function getinfo( ) {
		//Prompting for users information
		
	    var first_name = prompt( "Please enter your first name: " );
		var last_name = prompt( "Please enter your last name: " );
		var area_code = prompt( "Please enter your area code: " );
		var phone_number = prompt( "Please enter your phone number: " );
		var person = new Person( first_name, last_name, area_code, phone_number );
		return Person;
		}

or

function getinfo( ) {
		//Prompting for users information
		var person = new Person( first_name, last_name, area_code, phone_number );
		return Person;
		}

Do the #1 and you will need to change your constructor function to...

function Person(first, last, acode, phnumber) {
  ...
}

The reason is that you are using different form of function prototype. You are passing 4 arguments and you will need to change it.

And your line 54 should be...

var person1 = getinfo();

Because you are creating a new Person inside the function and return it, you simply call the function to get a new Person object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.