ashToronto
ashToronto

Reputation: 45

JS Making a multiplication table without console.log()

I want to build a multiplication table without the use of console.log() in the function. I am currently experiencing difficulty splitting the values. The whole function must be in pure JS. Not intended for use on the DOM. I want the output printed on the terminal.

function multiplicationTable (maxValue){

 var array = []

 for (var i = 1; i <= maxValue; i++)
 {
  for (var j = 1; j <= maxValue; j++)
 {
   if (j >= 1 && i >= 1)
   {
     array.push(j*i)
   }
 }

}

var m = array.join()
return m;

}

console.log(multiplicationTable(3));

Current Output: 1,2,3,2,4,6,3,6,9

Required Output:

1 2 3
2 4 6
3 6 9

I feel I have to use .split() or .splice(). I just can't pinpoint where to add the values.

Upvotes: 0

Views: 450

Answers (5)

ashToronto
ashToronto

Reputation: 45

function multiplicationTable(maxValue) {
  var array = []
  for (var i = 1; i <= maxValue; i++) {
    var t = []
    for (var j = 1; j <= maxValue; j++) {
      t.push(j * i)
    }
    array.push(t)
  }
  return array
}

multiplicationTable(3).forEach(x => console.log(x.join(" ")));

Upvotes: 0

Ele
Ele

Reputation: 33726

Return an object and then with the function forEach build the desired output.

function multiplicationTable(maxValue) {
  var result = {}

  for (var i = 0; i < maxValue; i++) {
    for (var j = 0; j < maxValue; j++) {
      result[`${j + 1} ${i + 1}`] = (j + 1) * (i + 1);
    }
  }

  return result;
}

var maxValue = 3;
var arr = multiplicationTable(maxValue)
Object.keys(arr).forEach((k) => {
  console.log(k, arr[k]);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Z Charnell
Z Charnell

Reputation: 1

Consider creating a new array for every loop of i, then storing them in the original array.

for (var i = 0; i < maxValue; i++) {
  array[i] = [];
  for (var j = 0; j < maxValue; j++) {
    array[i].push((j+1)*(i+1))
  }
}

The return type will then be an array of arrays, which you will have to loop over to console log.

var multTable = multiplicationTable(3);
for (i = 0; i < multTable.length; i++) {
  console.log(multTable[i].join(" ");
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

Instead of joining in the array, I would build and return a multidimensional array. Then you can use forEach to join and log when calling. Like,

function multiplicationTable(maxValue) {
  var array = []
  for (var i = 1; i <= maxValue; i++) {
    var t = []
    for (var j = 1; j <= maxValue; j++) {
      t.push(j * i)
    }
    array.push(t)
  }
  return array
}

multiplicationTable(3).forEach(x => console.log(x.join(" ")));

Upvotes: 0

George
George

Reputation: 2422

Here is your multiplication table. It is supported in browser dev console, but not as a code snippet here. Copy and paste it inside browser console.

let a = 0;
let b = 0;
const len = 4;
let output = [];
for(a=0;a<len;a++){
  output.push([]);
  for(b=0;b<len;b++){
    output[a].push(`${a} x ${b} = ${a*b}`);
  }
}
console.table(output);

enter image description here

Upvotes: 1

Related Questions