Show Powers

Shows powers of a number specified in the Javascript.

HTML

<html>
<body>
  <table id="powers">
  <!-- This will be automatically filled in using Javascript -->
  </table>
</body>
</html>

CSS

#powers{
  border-collapse: collapse;
}

.step{
  font-weight: bold;
  padding-right: 15px;
}

JavaScript

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function ShowPowersOf(i,j) {
	var tbl = document.getElementById("powers");
  tbl.innerHTML = ""; // Clear the inner text
  
  var lastResult = 1;
  // Loop and output
  for (p = 1; p<= j; p += 1){
    var iResult = lastResult * i; /* calc this iteration's result */
    
    // Output
    tbl.innerHTML += "<tr><td class='step'>STEP " + p + ":</td><td>" + numberWithCommas(lastResult) + " * " + i + " = " + numberWithCommas(iResult)+ "</td></tr>";
    
    // Reassign lastResult for next iter
    lastResult = iResult;
  }
}

ShowPowersOf(2,30);