Javascript 乘法 table 细节

Javascript multiplication table specifics

我正在尝试完成乘法 table 但 运行 遇到了问题,这是我的代码...

function multiTable(number) {
  var table = '';
  for (i = 1; i < 11; i++) {
    if (i == 1 || number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9) {
      table += i + " * " + number + " = " + (i * number) + "\n";
    } else if (i = 10) {
      table += i + " * " + number + " = " + (i * number);
    }
  }
  return table;
}

当我通过提供的测试时,我得到...

'1 * 5 = 5\n2 * 5 = 10\n3 * 5 = 15\n4 * 5 = 20\n5 * 5 = 25\n6 * 5 = 30\n7 * 5 = 35\n8 * 5 = 40\n9 * 5 = 45\n10 * 5 = 50\n'

我应该得到...

'1 * 5 = 5\n2 * 5 = 10\n3 * 5 = 15\n4 * 5 = 20\n5 * 5 = 25\n6 * 5 = 30\n7 * 5 = 35\n8 * 5 = 40\n9 * 5 = 45\n10 * 5 = 50'

为了节省时间,唯一的区别是最后,50 之后的\n。

我不知道这是否有帮助,但这是测试:

Test.describe("Basic tests",() => {
  Test.assertEquals(multiTable(5), '1 * 5 = 5\n2 * 5 = 10\n3 * 5 = 15\n4 * 5 = 20\n5 * 5 = 25\n6 * 5 = 30\n7 * 5 = 35\n8 * 5 = 40\n9 * 5 = 45\n10 * 5 = 50');
  })

function multiTable(number) {
    var table = '';
    for(var i = 1; i < 10; i += 1){ // print 9 times with \n
        table += i + " * " + number + " = " + (i * number) + "\n";
    }
    table += 10 + " * " + number + " = " + (10 * number); // and last line
    return table;
}

console.log(multiTable(5));
.as-console-wrapper { max-height: 100% !important; top: 0; }

我在这里找到了解决方案

function multiTable(number) {
var table = '';
for(i=1;i<11;i++){
if(i === 10){
table += i+ " * " +number+ " = " +(i*number);
}else{
table += i+ " * " +number+ " = " +(i*number)+ "\n";
}
}
return table;
}