从ASCII显示模式中的字符

时间:2015-03-19 11:22:03

标签: javascript ascii

与标题所说的一样,我们希望从ASCII值中显示模式中的字符集。我们提出了这个代码,但它不起作用......

for (var i = 1; i < 7; i++) {
            for (var j = 46; j < i + 1; j++) {
                document.write(String.fromCharCode(j));
            }
            document.write("<br>");
        }

        for(var k = 8; k > 1; k--) {
            for(var l = 97; l < k - 1; l++){
            document.write(String.fromCharCode(l));
            }
            document.write("<br>");
        }

输出应该是这样的:

A

AB

ABC

ABCD

ABCDE

ABCDEF

ABCDEFG

ABCDEFGH

ABCDEFGHI

ABCDEFGH

ABCDEFG

ABCDEF

ABCDE

ABCD

ABC

AB

1 个答案:

答案 0 :(得分:0)

试试这个..

for (var i = 1; i < 10; i++) {
            for (var j = 65; j < 65+i; j++) {
                document.write(String.fromCharCode(j));

            }
            document.write("<br>");
        }

        for(var k = 10; k > 1; k--) {

            for(var l = 97; l < 96+k; l++){

            document.write(String.fromCharCode(l));
            }
            document.write("<br>");
        }

结果:

A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
ABCDEFGHI
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a