尝试创建一个输出给定最大值的乘法表的函数。
我的问题是我有一个数组我需要加入一个字符串,无论我尝试什么,我都无法摆脱每个内部数组项之间的逗号,请帮助我!
这是我的代码:
var multiplicationTable = function(maxValue) {
var array = [];
var maxNum = maxValue ** 2;
y = 1;
var pad = ("+--" + "-".repeat(maxNum.toString().length - y.toString().length + 1)).repeat(maxValue) + "\+\n";
for (var i = 0; i <= maxValue * 2; i++) {
if (i % 2 != 0) {
array[i] = [];
for (var j = 0; j < maxValue; j++) {
var n = ((j + 1) * (i + 1)) / 2;
if (j + 1 == maxValue) {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1) + "|\n");
} else {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1));
}
};
} else {
array[i] = [pad];
}
}
return array.join(" ");
};
console.log(multiplicationTable(5));
console.log(multiplicationTable(10));
&#13;
答案 0 :(得分:3)
问题是你要连接数组而不是字符串。尝试将其转换为字符串并连接而不是附加到数组。
var multiplicationTable = function(maxValue) {
var array = [];
var maxNum = maxValue**2;
y = 1;
var pad = ("+--" + "-".repeat(maxNum.toString().length - y.toString().length + 1)).repeat(maxValue) + "\+\n";
for (var i = 0; i <= maxValue * 2; i++) {
if (i%2 != 0) {
array[i] = "";
for (var j = 0; j < maxValue; j++) {
var n = ((j + 1) * (i + 1)) / 2;
if (j + 1 == maxValue) {
array[i] += "| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1) + "|\n";
} else {
array[i] += "| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1);
}
};
} else {
array[i] = [pad];
}
}
return array.join(" ");
};
console.log(multiplicationTable(5));
console.log(multiplicationTable(10));
可生产
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
答案 1 :(得分:2)
我会“加入”内部数组,然后是外部数据。如果您不想更改大部分代码。
我刚刚更改了您的返回语句的功能:
return array.map(i => i.join("")).join("");
地图功能遍历所有元素,“转换” 数组 -Array为字符串 - 数组,通过内联函数,在每个项目上调用 join 函数 最后,字符串 -Array将与最终的 join 函数一起加入。
<强>更新强>
没有Space的 join 可以防止输出失真。
var multiplicationTable = function(maxValue) {
var array = [];
var maxNum = maxValue**2;
y = 1;
var pad = ("+--" + "-".repeat(maxNum.toString().length - y.toString().length + 1)).repeat(maxValue) + "\+\n";
for (var i = 0; i <= maxValue * 2; i++) {
if (i%2 != 0) {
array[i] = [];
for (var j = 0; j < maxValue; j++) {
var n = ((j + 1) * (i + 1)) / 2;
if (j + 1 == maxValue) {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1) + "|\n");
} else {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1));
}
};
} else {
array[i] = [pad];
}
}
return array.map(i => i.join("")).join("");
};
console.log(multiplicationTable(5));
console.log(multiplicationTable(10));
答案 2 :(得分:0)
您可以使用.replace(/,/g, '');
做这样的事情:
var multiplicationTable = function(maxValue) {
var array = [];
var maxNum = maxValue**2;
y = 1;
var pad = ("+--" + "-".repeat(maxNum.toString().length - y.toString().length + 1)).repeat(maxValue) + "\+\n";
for (var i = 0; i <= maxValue * 2; i++) {
if (i%2 != 0) {
array[i] = [];
for (var j = 0; j < maxValue; j++) {
var n = ((j + 1) * (i + 1)) / 2;
if (j + 1 == maxValue) {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1) + "|\n");
} else {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1));
}
};
} else {
array[i] = [pad];
}
}
var myTable = array.join(" ");
return myTable.replace(/,/g, '');
};
console.log(multiplicationTable(5));
console.log(multiplicationTable(10));
答案 3 :(得分:0)
工作小提琴https://jsfiddle.net/2097451n/6/
var multiplicationTable = function(maxValue) {
var array = [];
var maxNum = maxValue*2;
y = 1;
var pad = ("+--" + "-".repeat(maxNum.toString().length - y.toString().length + 1)).repeat(maxValue) + "\+\n";
for (var i = 0; i <= maxValue * 2; i++) {
if (i%2 != 0) {
array[i] = [];
for (var j = 0; j < maxValue; j++) {
var n = ((j + 1) * (i + 1)) / 2;
if (j + 1 == maxValue) {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1) + "|\n");
} else {
array[i].push("| " + n.toString() + " ".repeat((maxNum).toString().length - n.toString().length + 1));
}
};
// join nested array explicitly.
//
array[i] = array[i].join('')
} else {
array[i] = [pad];
}
}
return array.join("");
};
console.log(multiplicationTable(5));
console.log(multiplicationTable(10));