-1

I am trying to make a program in which can print a multiplication table (a typical one, like the ones found in notebooks in table order). This table uses inputs in which it defines the perimeter of each axis (number defines length of x axis, multiplier defines y-axis). However, it does not print.

Is there a typo in the code? Thanks.

Code:

function addNumbers() {
  var k;
  var l;
  for (i = 1; i < l; i++) {
    document.write("<tr>");
    for (j = 1; j < k; j++) {
      document.write("<td>" + j * i + "</td>");
    }
    document.write("</tr>");
  }
}
Enter the number : <input id="k" type="number" min="3" /> Enter the multiplier : <input id="l" type="number" max="11" />
<br>
<br>
<button onclick="addNumbers()">Print Multiplication table</button>
beginner
  • 63
  • 6

3 Answers3

2

Do you want something like this?

There are a lot of issues in your code, I corrected as far as I understood.

function addNumbers() {
  const k = document.getElementById("k").value;
  const l = document.getElementById("l").value;

  let html = `<table><tr><th>Number</th><th>Multiplier</th><th>Result</th></tr>`;

  for (j = 1; j <= l; j++) {
    html += `<tr><td>${k}</td>`;
    html += `<td>${j}</td><td>${k*j}</td>`;
    html += `</tr>`;
  }
  html += `</table>`;

  document.getElementById("output").innerHTML = html;
}
Enter the number : <input id="k" type="number" min="3" />
<br /> Enter the multiplier : <input id="l" type="number" max="11" />
<br>
<br>
<button onclick="addNumbers()">Print Multiplication table</button>

<div id="output"></div>
Shivam Sharma
  • 1,277
  • 15
  • 31
  • Yes, the original code has many problems, but this is a good direct answer (+1). It might help OP to explain why this way is better than using document.write. – Yogi May 16 '22 at 08:00
2

There are several things wrong in your code.

  1. document.write is a pretty bad way to manipulate your DOM unless you want to do very specific things. You want to use stuff like createElement and appendChild to be more explicit in what you are doing for both readability and maintainability.

  2. You reference var k, var l in your code and you assume they will take the value of the inputs with id k and l respectively automatically. This does not happen and you need to specify where the value comes from.

  3. "Print" is probably not the right way to call what you want to do. In HTML/JS, when people say "print", they probably mean console.log. What you are trying to do is to modify the HTML. (This isn't something wrong, but just a thing that will help others understand what you want to do better)

function addNumbers() {
  const k = Number(document.getElementById('k').value);
  const l = Number(document.getElementById('l').value);

  const table = document.createElement('table');
  for (i = 1; i <= l; i++) {
    const row = document.createElement('tr');
    for (j = 1; j <= k; j++) {
      const cell = document.createElement('td');
      cell.innerHTML = i * j;
      row.appendChild(cell);
    }
    table.appendChild(row);
  }
  document.getElementById('tableTarget').appendChild(table);
}
Enter the number : <input id="k" type="number" min="3" /> Enter the multiplier : <input id="l" type="number" max="11" />
<br />
<br />
<button onclick="addNumbers()">Print Multiplication table</button>
<div id="tableTarget"></div>
cSharp
  • 2,884
  • 1
  • 6
  • 23
0

Try this one.

function addNumbers() {
var k_ = document.getElementById('k').value;
var l_ =  document.getElementById('l').value;

for(let i = 1; i <= l_; i++) {
    const result = i * k_;
    console.log(`${k_} * ${i} = ${result}`);
}


}
Enter the number : <input id="k" type="number" min="3" />
Enter the range : <input id="l" type="number" max="11" />
<br>
<br>
<button onclick="addNumbers()">Print Multiplication table</button>
Fadi
  • 585
  • 4
  • 22