help with making object method

G

greenflame

I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.

function showDims(input,method) {
// Depending on 'method' is 1 then shows an alert showing the
dimensions of
// 'input' and writes to the webpage if method is 2.
var Dims = dims(input); // Get dimensions
if (method == 1) {alert("Rows: " + Dims[0] + "\n" + "Columns: " +
Dims[1]);}
if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}
function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);
}
test = [
[1,2,3],
[4,5,6],
[7,8,9]];
Test = new Matrix(test);
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);
 
J

Jc

greenflame said:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.
function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);
}
Test.showDims(1);

You should be seeing a javascript error regarding method not being
defined when you run that script. Ensure you look for javascript errors
when you get unexpected results, they can usually point you in the
right direction.

Try (untested):

this.showDims = function(method) { showDims(this.M, method); }
 
R

RobG

greenflame said:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.

function showDims(input,method) {
// Depending on 'method' is 1 then shows an alert showing the
dimensions of

Don't let posted code auto-wrap, do it yourself at about 70 characters
(makes cut'n paste for playing much easier...) :)
// 'input' and writes to the webpage if method is 2.
var Dims = dims(input); // Get dimensions

You don't define the dims() method anywhere that I can see. Probably
should have something like:

var Dims = [ input.rows, input.columns ];

Or maybe create a 'dims' method for your matrix object. But anyway,
when you call the function this way, 'input' is no longer a Matrix
object and has become just a plain array, so before the above line, insert:

input = new Matrix(input);
if (method == 1) {alert("Rows: " + Dims[0] + "\n" + "Columns: " +
Dims[1]);}
if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}
function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);

Here you haven't quite got the syntax right:

this.showDims = function (method) { showDims(this.M, method);}

Alternatively, you could make the showDims method local to the Matrix
object (so you could delete the one above):

this.showDims = function (method) {
if (method == 1) {
alert('Rows: ' + this.rows + '\nColumns: ' + this.columns);
} else if (method == 2) {
document.write('Rows: ' + this.rows + '<br>');
document.write('Columns: ' + this.columns + '<br>');
}
};
}
test = [
[1,2,3],
[4,5,6],
[7,8,9]];
Test = new Matrix(test);
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);

As a general comment, I would keep the two showDim methods completely
separate and have 'getDims' return an array of rows, columns then deal
with the output separately - consider your function may be called from
inside some other DOCTYPE where neither of your current syntaxes are
supported.

'dims' can become a property of the matrix:

...
this.dims = [ this.rows, this.columns ];
...

and you could get it as:

alert( Test.dims ) // gives '3,3' in your example.
document.write(...Test.dims[0]+' rows' ... Test.dims[1]+' columns'...

But that hardly seems worth the effort since you already have properties
for rows and columns.

Here's the full thing:

<script type="text/javascript">

// Don't need this if keep method local
function showDims(input,method) {

// Initialise input as a Matrix
input = new Matrix(input);
var Dims = [ input.rows, input.columns]; // Get dimensions

// Use an else here so skip second test if first true
if (method == 1) {
alert("Rows: " + Dims[0] + "\n" + "Columns: " + Dims[1]);
} else if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}

function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;

// Use the following line if keeping showDims external
// this.showDims = function (method) { showDims(this.M, method);}

// Delete the following declaration if using external
this.showDims = function (method) {
if (method == 1) {
alert('Rows: ' + this.rows + '\nColumns: ' + this.columns);
} else if (method == 2) {
document.write('Rows: ' + this.rows + '<br>');
document.write('Columns: ' + this.columns + '<br>');
}
};

}

test = [
[1,2,3],
[4,5,6],
[7,8,9]];

Test = new Matrix(test);
// Test = new Matrix( [ [1,2,3], [4,5,6], [7,8,9] ] );
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);
Test.showDims(2);

</script>
 
R

RobG

greenflame said:
So how do I test if a variable say test is a matrix object or not?

Your constructor is:

function Matrix(matrixarray) {
...
}

So you can test to see if the constructor was 'Matrix':

alert(
'typeof Test: ' + typeof Test
+ '\nIs Test a Matrix? ' + ( Matrix == Test.constructor )
);

Will return true if Test is a 'Matrix', false otherwise.
 
G

greenflame

With the array object I can make a new array by simply saying:

test = new Array(//length goes here//);

and then I can say like:

test[//index goes here//] = //what it equals goes here//;

I can also define an array with:

test = [//elements go here//];

I want to know if I can do a similar thing with my matrix object and if
so then how?
 
A

ASM

greenflame said:
With the array object I can make a new array by simply saying:

test = new Array(//length goes here//);

and then I can say like:

test[//index goes here//] = //what it equals goes here//;

I can also define an array with:

test = [//elements go here//];

I want to know if I can do a similar thing with my matrix object and if
so then how?

on my idea a matrix is an arrangement (or array) of
number of rows arrays of number of cols elements

matrix(rows,cols) = matrix(4,3) = 4 arrays each with 3 elements

<script type="text/javascript">

// ==== exo 1 ====
test = new Array(3);
test[0] = new Array('1','2','3');
test[1] = new Array('4','5','6');
test[2] = new Array('7','8','9');

test_Q = test.length * test[0].length;

alert('test : quantity = '+test_Q+'\nelmts = '+test);

// ==== exo 2 ====
matrix_a = 'a;1;2;3,b;4;5;6,c;7;8;9';
matrix_a = matrix_a.split(',');
matrix_a_w = matrix_a.length;
for(var i=0;i<matrix_a.length;i++)
matrix_a = matrix_a.split(';');
matrix_a_h = matrix_a[0].length;
matrix_a_Q = matrix_a_w * matrix_a_h;

alert('matrix_a : quantity = '+matrix_a_Q+'\nelmts = '+matrix_a);

// ==== exo 3 ====
function newMatrix(rows,cols) {
var matrix = new Array();
for(var i=0;i<rows;i++) matrix = new Array(cols);
return matrix;
}

var N = newMatrix(4,5);
var Q = N.length * N[0].length;
alert('new matrix : quantity = '+Q+'\nelmts = ['+N+']');

// ==== exo 4 ====
function Matrix(matrixarray,rows) {
var M = matrixarray.split(',');
var Cols = M.length/rows;
// alert(Cols);
var A = '<pre>Matrix =\n<u>';
for(var i=0;i<rows;i++) {
A += '| ';
var x = i*Cols;
for(var j=0;j<Cols;j++) A += M[(x+j)*1]+' | ';
A += '\n'
}
A += '<\/u><\/pre>';
document.write(A);
}
Matrix('01,02,03,04,05,06,07,08,09,10,11,12',4);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top