...

View Full Version : variable printing problems



Que
05-04-2008, 12:08 AM
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.



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:



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. :confused:

A1ien51
05-04-2008, 12:48 PM
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

Que
05-06-2008, 07:44 AM
Of course....Thank you so much, it makes sense :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum