troubleshooting Question

JavaScript: OO Magic Square

Avatar of beesknees_1978
beesknees_1978 asked on
JavaScriptHTMLAJAX
5 Comments1 Solution688 ViewsLast Modified:
Hi there,
I thought for an exercise that I create a OO Magic Square in JavaScript. I will test if the Magic Square is right by testing if the numbers in the boxes are correct.

I have a single function "Magic Square" which gets a reference to a form that consists of the 9 boxes of the magic square named box11 - box33 where the numbers indicate the row and column number of the box.

The Magic Square function has some additonal functions which create "groups" for the boxes, assign the fields to the "group" and then tests the sum of those boxes in the group.

Here is the structure of the Magic Square:
      - this.groups (Array, contains the list of group names)
      - this.group.add (Function, 1 parameter, adds new group to the array)
      - this.group.test (Function, 2 paramters, tests if group sum equals the value)
      - this.group.addfunction (Function, 2 parameters, adds field to a particular group)
      - this.check (Function, loops through each of the groups, sums all the field values for the group)

I have tried mutiple ways to place the Group Name Array however each time I call a new group all the previous fields in that group cannot be found.

Is this the best approach for achieving what I intend to do, or is there an alternative method using OO?

Thanks.


<html>
<head>
<script>
function MagicSquare(f)
{
    this.frm=document.forms[f];
    this.groups= new Array();
    this.group.add = ms_add_grp;
    this.group.addfield = ms_add_grp_fld;
    this.check = ms_check;
 
}
 
function ms_add_grp(itemname)
{
    this.group.fields = new Array();
    this.group[this.group.length]=itemname;	
}
 
 
function ms_add_grp(itemname,fldname)
{
    
    this.group[this.group.length].fields[this.group.fields.length]=itemname;	
}
 
 
function ms_check()
{
    for(var i=0;i<this.f.group.length;i++)
    {
	var sumcount = 0;
 
	for(var j=0;j<this.f.group.fields.length;i++)
    	{
	    sumcount = sumcount + this.group[this.group.length].fields[j].value;
 
	}
    }
 
}
 
</script>
</head>
<body>
<form name='frmmgsq'>
<input name='box11'><input name='box12'><input name='box13'><br/>
<input name='box21'><input name='box22'><input name='box23'><br/>
<input name='box31'><input name='box32'><input name='box33'><br/>
 
 
<button onclick='omagicsq.check();'>Check Magic Square</button>
</form>
<script>
	var omagicsq  = new MagicSquare("frmmgsq");
 
	omagicsq.group.add("row1");
	omagicsq.groupField("row1","box11");
	omagicsq.groupField("row1","box12");
	omagicsq.groupField("row1","box13");
	omagicsq.group.test("row1","15");
	
	omagicsq.group.add("row2");
	omagicsq.groupField("row2","box21");
	omagicsq.groupField("row2","box22");
	omagicsq.groupField("row2","box23");
	omagicsq.group.test("row2","15");
 
	omagicsq.group.add("row3");
	omagicsq.groupField("row3","box31");
	omagicsq.groupField("row3","box32");
	omagicsq.groupField("row3","box33");
	omagicsq.group.test("row3","15");
 
	omagicsq.group.add("col1");
	omagicsq.groupField("col1","box11");
	omagicsq.groupField("col1","box21");
	omagicsq.groupField("col1","box31");
	omagicsq.group.test("col1","15");	
 
	omagicsq.group.add("col2");
	omagicsq.groupField("col2","box12");
	omagicsq.groupField("col2","box22");
	omagicsq.groupField("col2","box32");
	omagicsq.group.test("col2","15");	
 
	omagicsq.group.add("col3");
	omagicsq.groupField("col3","box13");
	omagicsq.groupField("col3","box23");
	omagicsq.groupField("col3","box33");
	omagicsq.group.test("col3","15");	
 
</script>
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros