12 Replies - 3944 Views - Last Post: 23 October 2011 - 12:18 AM

#1 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Problem with Array

Posted 20 October 2011 - 08:39 AM

The array in the constructor function AcronymTable is supposed to be an array of Acronym objects. I may not have it initialized properly, but I'm not entirely sure. I'm trying to get it to display at least one of the objects that should be store in it, but nothing happens. The alert windows are added to trace where the script stops working. I've tried looking for how to create an array of objects but everything I've found just confuses me further.

function Acronym(acronym, definition) {
	this.a = acronym;
	this.define = definition;
	this.getA = function getAcronym() {
		return this.a;
	};
	this.getD = function getDefinition() {
		return this.define;
	};
	this.asString = function toString() {
		var out = "[" + a + ":" + define + "]";
		return out;
	};
}

function AcronymTable() {
	this.table = new Array();
	this.edit = function add(acronym, def) {
		window.alert("Table edit function.");
		var element = new Acronym(acronym, def);
		for ( var i = 0; i < this.table.length; i++) {
			this.table.splice(i, 0, element.getA());
			this.table.splice(i + 1, 0, element.getD());
		}
	};
	
	this.find = function lookup(acronym) {
		window.alert("Looking up acronym.");
		for ( var i = 0; i < table.length; i++) {
			if (this.table[i] === acronym) {
				return this.table[i + 1];
			} else {
				return "";
			}
		}
	};
	
	this.string = function toString() {
		window.alert("AcronymTable toString function.");
		var acronym = new String();
		for ( var i = 0; i < this.table.length; i++) {
			acronym = Acronym(this.table[i], this.table[i + 1]).asString + "\n";
			return acronym;
		}
	};
}

function initialize(theTable) {
	window.alert("Function initialize.");
	
	theTable.edit("XML", "eXtensible Markup Language");
	theTable.edit("SOAP", "Simple Object Access Protocol");
	theTable.edit("WSDL", "Web Service Description Language");
	theTable.edit("UDDI", "Universal Description, Discovery and Intergration");
	
	return theTable;
}

function display(theTable){
	window.alert("Display function.");
	var output;
	for (var i = 0; i < theTable.length; i++){
		output = theTable[i].string;
		window.alert(output);
	}
}

function run() {
	window.alert("Script is running.");
	var wsTable = new AcronymTable();
	wsTable = initialize(wsTable);
	display(wsTable);
//	var input = window.prompt("Enter an acronym (Enter q to quit):");
//	while (input !== q) {
//		wsTable.find(input.toUpperCase());
//	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Array

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 20 October 2011 - 10:25 AM

for ( var i = 0; i < this.table.length; i++) { what do you think is the initial value of this.table.length ?

despite that there is a bunch of things not coded properly
- do not define object methods in a Constructor except for very specific reasons. methods should be defined in the prototype.
- while setters and getters are fine, they are pointless in your case as this.a and this.define are public.
- unless AcronymTable() is supposed to be a Singleton, put the methods in the prototype. if it is one, use an object literal instead of a function.
- never create String objects.
- I have no idea why you use Array.splice() to add data to your storage array
- toString() is a magic method. if you want to use it, do it properly.
- the Acronym objects could be reduced to {"XML" : "eXtensible Markup Language"}

var storage = {
    data : {},
    add  : function (acronym, definition) {
        storage.data[acronym] = definition;
    },
    find : function (key) {
        if (key in storage.data) {
            return storage.data[key];
        }
        return null;
    }
};
// example
storage.add("XML", "extensible mark-up language");
storage.add("SOAP", "Simple Object Access Protocol");

window.alert(storage.find("SOAP"));
window.alert(storage.find("YAML"));

This post has been edited by Dormilich: 20 October 2011 - 10:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 20 October 2011 - 11:00 AM

How does this array store Acronym objects? That is why this is setup in the way that it is. It is supposed to be object oriented to a degree. I had a way to change the length of the array at first, but then it seemed useless. The array needs to be dynamic in nature because of the fact that I will have to add code to allow the user to add acronyms to it.
Also, could you point out what is not coded properly?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 20 October 2011 - 02:05 PM

View PostThePheonix21, on 20 October 2011 - 08:00 PM, said:

How does this array store Acronym objects? That is why this is setup in the way that it is. It is supposed to be object oriented to a degree. I had a way to change the length of the array at first, but then it seemed useless. The array needs to be dynamic in nature because of the fact that I will have to add code to allow the user to add acronyms to it.

Javascript is object oriented to begin with. when you write procedural code you’re just not aware of the fact you’re already using objects.
And while JS is object oriented to begin with, it is also highly dynamic as well. (only recent additions (property descriptors) add some means to limit this dynamic nature a bit). the same is true for arrays.
besides that, you change the length of an array automatically by adding elements, not by changing the length property (which is read-only).
to add something to an array in object oriented style, use an array method, namely Array.push().


View PostThePheonix21, on 20 October 2011 - 08:00 PM, said:

Also, could you point out what is not coded properly?

you see that long list of mine?
Was This Post Helpful? 0
  • +
  • -

#5 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 21 October 2011 - 12:54 PM

The way it's setup is the way it is supposed to be, AcronymTable and Acronym are supposed to be constructor functions with the properties that are currently present.

This post has been edited by JackOfAllTrades: 22 October 2011 - 04:18 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 21 October 2011 - 01:29 PM

View PostThePheonix21, on 21 October 2011 - 09:54 PM, said:

The way it's setup is the way it is supposed to be, AcronymTable and Acronym are supposed to be constructor functions with the properties that are currently present.

if you say so, then I have to adhere …

but I won’t limit myself to just constructor functions when there are better (for the case) options available.
Was This Post Helpful? 0
  • +
  • -

#7 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 21 October 2011 - 02:03 PM

Ok, so far I can get the array populated, problem is now displaying the elements properly. As it stands, there is a problem in my toString methods that I'm not seeing. Could someone point it out to me?

function Acronym(acronym, definition) {
	this.a = acronym;
	this.define = definition;
	this.getA = function getAcronym() {
		return this.a;
	};
	this.getD = function getDefinition() {
		return this.define;
	};
	this.asString = function toString() {
		var out = "[" + a + ":" + define + "]";
		return out;
	};
}

function AcronymTable() {
	this.table = {};
	this.edit = function add(acronym, def) {
		//window.alert("Table edit function.");
		this.table[acronym] = def;
	};
	
	this.find = function lookup(acronym) {
		window.alert("Looking up acronym.");
			if (acronym in this.table) {
				return this.table[acronym];
			}
	};
	
	this.string = function toString() {
		var acronym = new String();
		for ( var name in this.table) {
			acronym = Acronym(this.table[name]).asString + "\n";
			return acronym;
		}
	};
}

function initialize(theTable) {
	window.alert("Function initialize.");
	
	theTable.edit("XML", "eXtensible Markup Language");
	theTable.edit("SOAP", "Simple Object Access Protocol");
	theTable.edit("WSDL", "Web Service Description Language");
	theTable.edit("UDDI", "Universal Description, Discovery and Intergration");
	
	return theTable;
}

function display(theTable){
	window.alert("Display function.");
	for (var acronym in theTable){
		window.alert(theTable[acronym].toString);
	}
}

function run() {
	window.alert("Script is running.");
	var wsTable = new AcronymTable();
	wsTable = initialize(wsTable);
	display(wsTable);
//	var input = window.prompt("Enter an acronym (Enter q to quit):");
//	while (input !== q) {
//		wsTable.find(input.toUpperCase());
//	}
}


This post has been edited by ThePheonix21: 21 October 2011 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 21 October 2011 - 02:58 PM

Ok, so in terms of displaying the array, the alert window pops up with any code that is currently in the AcronymTable function. The first window shows the table property, the second displays the code for the add property, the third for the lookup property, and so on. I'm starting to think that the problem is with the initialization function, something may not be working properly there.

This post has been edited by JackOfAllTrades: 22 October 2011 - 04:19 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 21 October 2011 - 11:53 PM

Quote

var out = "[" + a + ":" + define + "]";

neither a nor define are defined variables, use this.a and this.define instead.

Quote

The first window shows the table property, the second displays the code for the add property, the third for the lookup property, and so on. I'm starting to think that the problem is with the initialization function

what you see is absolutely correct, for…in loops over the properties of an object and the properties of the AcronymTable object are table, edit, find and string.
Was This Post Helpful? 0
  • +
  • -

#10 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 22 October 2011 - 07:25 AM

View PostDormilich, on 22 October 2011 - 12:53 AM, said:

Quote

var out = "[" + a + ":" + define + "]";

neither a nor define are defined variables, use this.a and this.define instead.

Quote

The first window shows the table property, the second displays the code for the add property, the third for the lookup property, and so on. I'm starting to think that the problem is with the initialization function

what you see is absolutely correct, for…in loops over the properties of an object and the properties of the AcronymTable object are table, edit, find and string.


What I need to display are the values contained in theTable. Right now it runs up through the initialization function and the add property of AcronymTable, but it crashes during the display method. I have a feeling the problem is in the initialization function, since I tried to get theTable to display the elements in the initialization function. Everything displays properly in the add property of AcronymTable, but it doesn't seem to be saving it to the actual table.

function Acronym(acronym, definition) {
	this.a = acronym;
	this.define = definition;
	this.getA = function getAcronym() {
		return this.a;
	};
	this.getD = function getDefinition() {
		return this.define;
	};
	this.asString = function toString() {
		var out = "[" + this.a + ":" + this.define + "]";
		return out;
	};
}

function AcronymTable() {
	this.table = [];
	this.edit = function add(acronym, def) {
		window.alert("Editing...");
		this.table[acronym] = def;
	};
	
	this.find = function lookup(acronym) {
		window.alert("Looking up acronym.");
			if (acronym in this.table) {
				return this.table[acronym];
			}
	};
	
	this.string = function toString() {
		/*for ( var name in this.table) {
			
		}*/
	};
}

function initialize(theTable) {
	window.alert("Initializing...");
	
	theTable.edit("XML", "eXtensible Markup Language");
	theTable.edit("SOAP", "Simple Object Access Protocol");
	theTable.edit("WSDL", "Web Service Description Language");
	theTable.edit("UDDI", "Universal Description, Discovery and Intergration");
	
	for (var name in theTable){
		window.alert(name + ": " + theTable[name]);
	}
	
	return theTable;
}

function display(theTable){
	window.alert("Display function.");
	for (var acronym in theTable){
		window.alert(theTable[acronym]);
	}
}

function run() {
	window.alert("Script is running.");
	var wsTable = new AcronymTable();
	wsTable = initialize(wsTable);
//	display(wsTable);
//	var input = window.prompt("Enter an acronym (Enter q to quit):");
//	while (input !== q) {
//		wsTable.find(input.toUpperCase());
//	}
}


Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 22 October 2011 - 07:49 AM

you try to look for the acronyms in the wrong place. the data are saved in the table property of AcronymTable, yet you look in the AcronymTable instance for it.
Was This Post Helpful? 0
  • +
  • -

#12 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: Problem with Array

Posted 22 October 2011 - 10:16 AM

View PostDormilich, on 22 October 2011 - 08:49 AM, said:

you try to look for the acronyms in the wrong place. the data are saved in the table property of AcronymTable, yet you look in the AcronymTable instance for it.


Is there a way I could actually get it to look in the table for the acronyms? If you look at the run function, there is an AcronymTable object called wsTable, that is what I need to display using the display function. I need to pass wsTable to the display function to be displayed. In the end I want to display all of the acronyms in one alert window, each on a separate line, but right now I would be happy if I could get it to display each in its own alert window.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem with Array

Posted 23 October 2011 - 12:18 AM

as I said, look in the place where the acronyms are stored. for (var acronym in theTable.table){
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1