skip to Main Content

please help me to figure out how to organize my result in rows of 10 numbers and create the look multiplication tables from 1 to 10 (SEE PICTURE SAMPLE OUTPUT)

SAMPLE OUTPUT

I was asked to modify the following for loop (see below) to print out a table horizontally and write the function to execute multiple tables:


<script>
const multiplier = 2;
for(let i=i, i<=10; i++){
    console.log(i*multiplier);
}
</script>

I got the for loop modified and it works (see below my code) but I get a long string of numbers:

let multiplier = 1;
let locationTables = '';

const tables = function tables(){
    for(let y=1; y<= 10; y++){
    let multiplication = y*multiplier;
    locationTables += multiplication + ' ';
    }; 
    return multiplier +=1;
}

const factory = function factory(f){
    while(multiplier <= 10){
        let subtable = f();
        
    }
}

var factortables = factory(tables);
console.log(locationTables);

MY OUTPUT

I want to organize then in rows of 10 numbers and 10 columns (table 10×10).

3

Answers


  1. You can add a new line to each output but then the are still not aligned. You can use padStart to fix this.

    let multiplier = 1;
    let locationTables = '';
    
    const tables = function tables(){
        for(let y=1; y<= 10; y++){
        let multiplication = y*multiplier;
        // padStart is used to prepend a string until it has it's required length
        locationTables += multiplication.toString().padStart(2, ' ') + ' ' ;
        };
        // Add new line after a full row 
        locationTables += 'n';
        return multiplier +=1;
    }
    
    const factory = function factory(f){
        while(multiplier <= 10){
            let subtable = f();
            
        }
    }
    
    var factortables = factory(tables);
    console.log(locationTables);
    Login or Signup to reply.
  2. Here is an alternative way:

    const res=Array(10).fill(0).map((_,i)=>
     Array(10).fill(0).map((__,j)=>String((i+1)*(j+1)).padStart(3," ")).join("")).join("n");
    console.log(res);
    Login or Signup to reply.
  3. Try this. Instead of building up string we can push each value into an array within a main array. Each array is a "row" of data.

    Ad the end you can either view the raw array data or format each row as a string

    let multiplier = 1;
    const max = 10;
    const maxDigits = (max * max).toString().length + 1;
    let rowIndex = 0; 
    let locationTables = [[]];
    
    const tables = function tables(){
       
      for(let y=1; y<= max; y++){
        let multiplication = y * multiplier;
        locationTables[rowIndex].push(multiplication);
      };
      if(rowIndex < max-1){
        rowIndex++;
        locationTables[rowIndex] = [];
      }
      return multiplier +=1;
    }
    
    const factory = function factory(f){
      while(multiplier <= max){
        let subtable = f();
      }
    }
    
    var factortables = factory(tables);
    //console.log('raw arrays', locationTables);
    
    const grid = locationTables.map(row => {
      return row.map(d => {
        return d.toString().padStart(maxDigits, ' ');
      }).join('')
    }).join('n');
    console.log(grid);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search