user1613478 user1613478 - 26 days ago 11
Javascript Question

I want to count rows and columns in JS

I want to use Table class as can be easily implemented in JavaScript as shown in the following code snippet. After having declared the object, we can instantiate it by using the new operator and use its properties and methods.But my code is not working. Please have a look.



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>:Functions as Variables:</title>

<style>
table {margin-bottom:15px;}

td {padding:10px; text-align:center; border:1px solid #cecece;}
</style>

<script src="js/jquery-1.11.3.min.js"></script>
</head>
<body>

<table cellpadding="0" cellspacing="0" width="100%" border="0">
<tr>
<td>1</td>
<td>2</td>
</tr>

<tr>
<td>3</td>
<td>4</td>
</tr>

<tr>
<td>5</td>
<td>6</td>
</tr>

<tr>
<td>7</td>
<td>8</td>
</tr>

<tr>
<td>9</td>
<td>10</td>
</tr>

<tr>
<td>11</td>
<td>12</td>
</tr>

<tr>
<td>13</td>
<td>14</td>
</tr>

</table>



<script>
function table(row,column) {
this.row = row;
this.column = column;
getCountCell = function(){
return this.rows * this.column;
}
}

var c = new table(3,5);

var t = c.getCountCell();


console.log(t);




</script>
</body>
</html>





Answer

spelling mistakes is a row not rows and apply this in front of this.getCountCell

  function table(row,column) {
    this.row = row;
    this.column = column;
    this.getCountCell = function(){
    return this.row * this.column;
    }	
    }
    
    var c = new table(3,5);
    
    var t = c.getCountCell();
    
    
    console.log(t);
table {margin-bottom:15px;}
    
    td {padding:10px; text-align:center; border:1px solid #cecece;}
    
    <table cellpadding="0" cellspacing="0" width="100%" border="0">
    <tr>
    <td>1</td>
    <td>2</td>
    </tr>
    
    <tr>
    <td>3</td>
    <td>4</td>
    </tr>
    
    <tr>
    <td>5</td>
    <td>6</td>
    </tr>
    
    <tr>
    <td>7</td>
    <td>8</td>
    </tr>
    
    <tr>
    <td>9</td>
    <td>10</td>
    </tr>
    
    <tr>
    <td>11</td>
    <td>12</td>
    </tr>
    
    <tr>
    <td>13</td>
    <td>14</td>
    </tr>
    
    </table>
    
  
    

Comments