那么我怎样才能制作多个字母呢?我正在使用一个包含字符串的数组,我想将它们乘以它们的索引。
A
BB
CCC
DDDD
EEEEE
等等
<script>
window.onload = start;
var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26];
function start() {
document.getElementById("randomize").onclick = execute;
}
function execute() {
for (var i = 0; i < letters.length; i++) {
for (var j = 0; j <= numbers.length; j++) {
var product = numbers.length * letters[i];
document.getElementById("output").innerHTML += "<li>" + letters[i] + "</li>";
}
}
}
</script>
最佳答案
for(var i = 0; i < 26; i++){
console.log(String.fromCharCode(65 + i).repeat(i + 1));
}
给你
A
BB
CCC
...
一个更短的:
var i = 0;
while(i++ < 26) console.log(String.fromCharCode(64 + i).repeat(i));
关于javascript - 如何根据字母制作金字塔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46845416/