javascript - JS 不用console.log()制作乘法表

标签 javascript

我想在函数中不使用 console.log() 的情况下构建乘法表。我目前在分割值(value)观方面遇到困难。整个函数必须是纯JS的。不适用于 DOM。我想要在终端上打印输出。

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));

当前输出:1,2,3,2,4,6,3,6,9

所需输出:

1 2 3
2 4 6
3 6 9

我觉得我必须使用.split()或.splice()。我只是无法确定在哪里添加值。

最佳答案

这是你的乘法表。它在浏览器开发控制台中受支持,但此处不作为代码片段。将其复制并粘贴到浏览器控制台中。

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

关于javascript - JS 不用console.log()制作乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247421/

相关文章:

javascript - 从具有特定条件的 div 中获取文本

javascript - Web 应用程序可以针对浏览器扩展进行保护吗?

javascript - 从外部 iframe 启动全屏

javascript - ModelAndView 不重定向,但给出正确的响应

javascript - 对于某些提交,Google Analytics 事件跟踪失败

Javascript 仅在放置在 View 页面中时才起作用

javascript - 动态更改元素的子属性数组

javascript - 在预期返回 void 的函数参数中返回 Promise

javascript - 使用knockout js绑定(bind)下拉值

javascript - 如何使用jQuery检测页面的滚动位置