Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2008, 12:08 AM   PM User | #1
Que
New Coder

 
Join Date: Apr 2008
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Que is an unknown quantity at this point
variable printing problems

Hi everyone, i'm working on a little project where i'm supposed to use objects to print various things to an HTML document.
The main restriction i have is to use a single document.write function after calling the main function to print every single thing to the website.

Heres my code so far.

Code:
var partOneHeader;
var partOne;
var space;
var partTwoHeader ="<b> Speakers</b><br/>";
var partTwo;
var partThreeHeader ="<b> Computers</b><br/>";
var partThree;
var content;

function writeBody()				//This you could say is the central hub of the program, from here we'll call all the neccessary functions
{			
	//partOneHeader = document.write("<b>Televisions</b><br/>");						//in order to write all the needed things.
	partOne = printTelevision();				//This particular function writes the information on the televisions.
	space = document.write("<br>");
	//partTwoHeader = document.write("<b> Speakers </b><br/>");
	partTwo = printSpeaker();
	//partThreeHeader = document.write("<b>Computers</b><br/>");
	partThree = printComputer(); 
	//content = partOneHeader;
	//return content;
}

function generateOutPut()			
{					
	document.write(writeBody());
								
}

function printTelevision()			
{
	for(i=0; i < tv.length; i++)			//We use a for loop to write out the information contained in the tv array.
	{
		if (i == 0) 
		{
			document.write("<b>Televisions</b><br/>");
		}
		document.write(tv[i]);
	}
	document.write("<br/>");
}

function printSpeaker()
{
	for(i=0; i < spkr.length; i++)
	{
		if(i==0)
		{
			document.write("<b>Speakers</b><br/>");
		}
		document.write(spkr[i]);
	}
	document.write("<br/>");
}

function printComputer()
{
	for(i=0; i < pc.length; i++)
	{
		if( i == 0)
		{
			document.write("<b><br/>Computers</b><br/>");
		}
		document.write(pc[i]);
	}
	document.write("<br/>");
}

function televisionToString() 
{
	return "<br><b>Brand: </b>" + this.brand + "<br><b>Screen size: </b> " + this.screen + " inches" + "<br><b>Price: </b>$" + this.price
}

function computerToString()
{
	return "<br><b>Brand: </b>" + this.brand + "<br><b>Speed: </b> " + this.speed+ "<br><b>Ram: </b>" + this.ram+ "<br><b>Hard Disk Drive Space: </b>" +this.hdd+ "<br><b>Price: </b>$"+this.price
}

function speakerToString()
{
	return "<br><b>Brand: </b>" + this.brand + "<br><b>Power: </b> " + this.power+ "<br><b>Price: $</b>" + this.price
}


function television(brand, screen, price) {
	this.brand = brand;
	this.screen = screen;
	this.price = price;
	this.toString = televisionToString;
}


function computer(brand, speed, ram, hdd, price)
{
	this.brand = brand;
	this.speed = speed;
	this.ram = ram;
	this.hdd = hdd;
	this.price= price;
	this.toString= computerToString;
}

function speaker(brand, power, price)
{
	this.brand = brand;
	this.power= power;
	this.price= price;
	this.toString = speakerToString;
}

var tv = new Array();									//as per on our earlier ICEs, store the function Television in an array.
tv[0] = new television("Sharp", "46", "2999.99");
tv[1] = new television("Samsung", "40", "1299.99");
tv[2] = new television("Sony", "52", "4299.98");
tv[3] = new television("Toshiba", "19", "449.99");

var spkr = new Array();
spkr[0] = new speaker("Bose", "330W", "440");
spkr[1] = new speaker("JBL", "220W", "300");
spkr[2] = new speaker("Ohm", "300W", "350");
spkr[3] = new speaker("Paradigm", "390W", "400");

var pc = new Array();
pc[0] = new computer("HP", "2.0ghz", "2GB", "100GB", "1200")
pc[1] = new computer("Sony", "3.0ghz", "4GB", "200GB", "3400")
pc[2] = new computer("Acer", "2.5ghz", "3GB", "150GB", "2000")
pc[3] = new computer("Alienware", "4.0ghz", "4GB", "500GB", "5000")
The first thing that is called is generateOutPut();
Now while it does print to the website fine and all, i'm getting a little undefined bieng printed at the end of the document.

As you look through the code, you'll find i tried to use variables and add them as strings, but the problem i was having was that when i assigned functions to the variables, the function itself was printing to the website before i could even add the variables together as string and then return a variable called content that i could use to print the entire thing to the document.

In summary, i want to be able to write:

Code:
function generateOutPut()
{
         writeBody();          // have variable content returned from this
         document.write(content);        //print the full list to the website
}
I've tried several variations, but for some reasons, the functions defined in the variables get printed before i can even add them as strings.
Que is offline   Reply With Quote
Old 05-04-2008, 12:48 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
well you are still using document.write inside of the speaker and computer functions. With the way you coded it, you should be returning a string in those functions.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Users who have thanked A1ien51 for this post:
Que (05-06-2008)
Old 05-06-2008, 07:44 AM   PM User | #3
Que
New Coder

 
Join Date: Apr 2008
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Que is an unknown quantity at this point
Of course....Thank you so much, it makes sense
Que is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:37 PM.


Advertisement
Log in to turn off these ads.