//Problem 1 function Counter(c=0){ var count = c ; this.inc = function() { count++ ;} this.getCount = function(){ return count; } } //to make sure that we can call print on an instance of Counter objects we need // to implement a specific code for the 'toString' method that it inherents from the // ancestor object 'Object' Counter.prototype.toString = function(){ return this.getCount(); } //Problem 2 /* Question 2.1 Matrix.prototype.toString() { var line="" ; for(var i =0 ; i < this.data.length ; i++) { line += this.data[i] + " " ; if( (i+1) % this.dim[1] == 0 ) line += "
" ; //new line at end of row } return line ; } */ // The following code contains the answers to questions 2.2, 2.3 and 2.4 //Matrix Library /*The matrix constructor Input: dim :: array of 2 integers data:: array of all matrix elements from left->right and top->bottom "Output": (not really a return value, mind you!) Object with properties: dim :: an array of 2 integers data:: a 1-dimensional array w/ actual elements sorted left->right,top->bottom */ function Matrix(dim=[0,0], data=[], name="noname"){ this.dim = dim ; this.data = data ; this.name = name; //consistency test v--- Question 2.3 if( this.dim[0]*this.dim[1] != this.data.length){ var msg="ERROR:"+ " matrix "+this.name+" :"+ " Incompatible data length ("+this.data.length+")"+ " w/ matrix dimensions ("+this.dim[0]+"x"+this.dim[1]+") "; console.log(msg); alert(msg); //only in browser } } // Question 2.2 Matrix.prototype.toString = function(){ var nl = "
"; var om = "[ "; var cm = " ]"; var line=this.name+om ; for(var i=0 ; i