怎么用js里面的字符串打印出由字母组成的倒三角形?

要这样的a b c d e f a b c d e a b c d ... .. .
2025-03-18 09:33:47
推荐回答(1个)
回答1:



        function printTri(x)
        {
            //放进段落,段落居中,自然就居中了
            document.write("");            
            for (var i = 1; i <= x; i++)
            {                
                var arr = [ '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', '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 pos  =  0;
                for (var j = 1; j <= x - i + 1; j++)
                {
                    document.write(" ");
                    pos = Math.round(Math.random() * (arr.length - 1));                    
                    var icon = arr[pos];
                    document.write(" " + icon +" ");
                }
                document.write("
");
            }
            document.write("

");

        }
        printTri(10);    

可以使用以上代码实现,请采纳。

效果如下: