Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to use a for loop in javascript to create a a table that displays * and for each row the columns increase by 1 *

***let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
    for (let column = 1; column <= numColumns; column++) {
        strRowOutput += "*";
    }
    console.log(strRowOutput);
    strRowOutput = "";
}
console.log();***

thats the code so far but it only displays the last row with 10*.enter image description here

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The second for loop condition should be dependent to the variable in first for loop like column <= row OR if you want to restrict the column to a maximum value, you can use column <= Math.min(row, numColumns)

Im using the second approach here

Working fiddle

let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
  for (let column = 1; column <= Math.min(row, numColumns); column++) {
      strRowOutput += "*";
  }
  console.log(strRowOutput);
  strRowOutput = "";
}
console.log();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading