Table row count incorrectly reported after appending a row

Table row count incorrectly reported after appending a row

I have an existing table in html.  See below:

<table id="test_table">
    <tr>
        <td>Col1 Row1</td><td>Col2 Row1</td>
    </tr>
    <tr>
        <td>Col1 Row2</td><td>Col2 Row2</td>
    </tr>
</table>

I then use jquery to append another row using the following function:

function append_last_row_to_table(table_name, cell_contents) {
    var last_row_idx = get_number_of_table_rows(table_name);
    $("#" + table_name + " tr:nth-child(" + last_row_idx + ")").after(cell_contents);
}

I then use jquery to return the number of rows using the following function:

function get_number_of_table_rows(table_name) {
    return $("#" + table_name + " tr").length;
}

The number of rows returned is 2 both before appending a row (which is correct) and after appending a row (should be 3).

I would be grateful if anyone could suggest what I am doing wrong.

Thank you.