Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created July 9, 2014 00:02
Show Gist options
  • Save jikeytang/6ffd12f97d438e4da539 to your computer and use it in GitHub Desktop.
Save jikeytang/6ffd12f97d438e4da539 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140709-题目1
用速度最优的代码输出一个用"A"拼凑而成的平行四边形。
A
AAA
AAAAA
AAAAAAA
AAAAAAAAA
AAAAAAA
AAAAA
AAA
A
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@hjzheng
Copy link

hjzheng commented Jul 9, 2014

var reCommentContents = /\/\*!?(?:\@preserve)?\s*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)\s*\*\//;
var multiline = function (fn) {
    if (typeof fn !== 'function') {
        throw new TypeError('Expected a function.');
    }

    var match = reCommentContents.exec(fn.toString());

    if (!match) {
        throw new TypeError('Multiline comment missing.');
    }

    return match[1];
};

var str = multiline(function(/*
                    A
                   AAA
                  AAAAA
                 AAAAAAA
                AAAAAAAAA
                 AAAAAAA
                  AAAAA
                   AAA
                    A
*/){});

console.log(str);

@liuqiongqiong
Copy link

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>菱形A显示</title>
    <style type="text/css">
        #ul1{width: 200px;height: 300px;}
        #ul1 li{list-style: none;text-align: center;}
    </style>
</head>
<body>
<div>
    <ul id="ul1"></ul>
</div>
</body>
<script type="text/javascript">
window.onload = function(){
    function showA(num){
        var a = new Array(num+1).join('A');
        var arr = new Array(num);
        for(var i=0; i< num / 2; i++){
            arr[i] = arr[num-1-i] = '<li>'+a.substr(0, 2*i+1)+'</li>';
        }        
        document.getElementById('ul1').innerHTML = arr.join('');
    }
    showA(9);    
}
</script>
</html>

@heqing0712
Copy link

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>[ Javascript ] - 20140709-题目1</title>
    <style type="text/css">
        body{line-height:1.2em;text-align:center;}
    </style>
</head>

<body>
    <div id="cn"></div>
      <script type="text/javascript">
        (function (n,c) {
            var a = new Array(n+1).join(c), s = '', m = 0;
            while (n - 1) {
                s+= a.substr(0, ++m >n ? --n : m) + "<br/>";
            }
            document.getElementById('cn').innerHTML = s;

        }(8,'A'));
    </script>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment