• ベストアンサー

ABCD・・・Z と表示しようとして

<script> for(var i='A';i<='Z';i++) { document.write(i); } </script> としましたが結果は A となりました。 ABCD・・・Z と表示するにはどうしたらいいでしょうか? Perlのように簡単にはいかないのでしょうか?

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

Perl殆どやってないけどね。 規格を読んで見ましょうか。(該当部分は抜き出すけど訳さないよ) BNFは習得済みとします。 http://www.atmarkit.co.jp/fxml/ddd/ddd004/ddd004-bnf.html ECMA-262 http://www.ecma-international.org/publications/standards/Ecma-262.htm 11.3 Postfix Expressions Syntax Postfix Expression : LeftHandSideExpression LeftHandSideExpression [no LineTerminatorhere] ++ LeftHandSideExpression [no LineTerminatorhere] -- 11.3.1 Postfix Increment Operator The production PostfixExpression : LeftHandSideExpression [no LineTerminator here] ++ is evaluated as follows: 1. Evaluate LeftHandSideExpression. 2. Call GetValue(Result(1)). 3. Call ToNumber(Result(2)). 4. Add the value 1 to Result(3), using the same rules as for the + operator (see 11.6.3). 5. Call PutValue(Result(1), Result(4)). 6. Return Result(3). ================================ 9.3.1 ToNumber Applied to the String Type ToNumber applied to strings applies the following grammar to the input string. If the grammar interpret the string as an expansion of StringNumericLiteral, then the result of ToNumber is NaN. ================================ 11.6.3 Applying the Additive Operators ( +,- ) to Numbers The + operator performs addition when applied to two operands of numeric type, producing the sum of the operands. The - operator performs subtraction, producing the difference of two numeric operands. Addition is a commutative operation, but not always associative. The result of an addition is determined using the rules of IEEE 754 double-precision arithmetic: * If either operand is NaN, the result is NaN. (以下略) ===================================== 11.8.3 The Less-than-or-equal Operator ( <= ) The production RelationalExpression : RelationalExpression <= ShiftExpress 1. Evaluate RelationalExpression. 2. Call GetValue(Result(1)). 3. Evaluate ShiftExpression. 4. Call GetValue(Result(3)). 5. Perform the comparison Result(4) < Result(2). (see 11.8.5). 6. If Result(5) is true or undefined,return false.Otherwise,return true. ========================================= 11.8.5 The Abstract Relational Comparison Algorithm The comparison x < y,where x and y are values, produces true, false,or undefi at least one operand is NaN). Such a comparison is performed as follows: (以下略) ================ よって var i = "A"; i++; を実行すると 【iはNaNになります。】 i < 'Z'; は片方がNaNであればNaNとなります。 ==================== 9.3 ToBoolean The operator ToBoolean converts its argument to a value of type Boolean according to the following table: (略) The resultis false if the argumentis +0, -0,or NaN;otherwise the resultis true. ====================== 12.6.3 The for Statement (略) The production IterationStatement : for ( var VariableDeclarationListNoIn ; Expressionopt ; Expressionopt ) Statement is evaluated as follows: 1. Evaluate VariableDeclarationListNoIn. 2. Let V = empty. 3. If the first Expression is not present, go to step 8. 4. Evaluate the first Expression. 5. Call GetValue(Result(4)). 6. Call ToBoolean(Result(5)). 7. If Result(6) is false,gotostep14. 8. Evaluate Statement. 9. If Result(8).value is not empty, let V = Result(8).value. 10.If Result(8).type is break and Result(8).target is in the current label set, go to step 17. 11.If Result(8).type is continue and Result(8).target is in the current label set, go to step 13. 12.If Result(8) is an abrupt completion, return Result(8). 13.If the second Expression is not present, go to step 3. 14.Evaluate the second Expression. 15.Call GetValue(Result(14)). (This value is not used.) 16.Go to step 3. 17.Return (normal,V, empty). =================== ということで2回目の真ん中はNaNはfalseで,右側が実行されるだけで,中身は実行されません 結果として最初の1回だけ実行されるんですね。 ===================== さて,解決策ですが,ASCII互換のUnicodeではAからZは連続しているので 15.5.3.2 String.fromCharCode([char0[,char1[,…]]]) Returns a string value containing as many characters as the number of arguments. Each argument specifies one character of the resulting string, with the first argument specifying the first character, and so on, from left to right. An argument is converted to a character by applying the operation ToUint16 (9.7) and regarding the resulting 16-bit integer as the code point value of a character. If no arguments are supplied, the result is the empty string. The length property of the fromCharCode function is 1. 15.5.4.5 String.prototype.charCodeAt (pos) 16 Returns a number (a nonnegative integer less than 2 ) representing the code point value of the character at position pos in the string resulting from converting this object to a string. If there is no character at that position, the result is NaN. When the charCodeAt method is called with one argument pos, the following steps are taken: 1. Call ToString, giving it the this value as its argument. 2. Call ToInteger(pos). 3. Compute the number of characters in Result(1). 4. If Result(2) is less than 0 or is not less than Result(3), return NaN. 5. Return a value of Number type, whose value is the code point value of the character at position Result(2) in the string Result(1), where the first (leftmost) character in Result(1) is considered to be at position 0, the next one at position 1, and so on. の二つを元に for (var i = "A".charCodeAt(0);i <= "Z".charCodeAt(0);i++){ document.writeln(String.fromCharCode(i)); } という形で書けば,ソースコード上にAからZまで改行区切りで出力されます(lnがなければ,改行されない) HTMLとソース中の空白文字類とCSSの関係(改行しているように見えないんだけどーとか聞くな) CDATAマーク区間の話 document.writelnの仕様(DOM Level 2 HTML,HTML5) 7.8.4 String Literals prototypeとは何か については説明しません。

reiman
質問者

お礼

ありがとうございます。 今回は var aa='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'.split(','); としました。 配列のすることが狙いだったので不細工ですが案外短かったので これでいきました。 ご提示の方法は応用が効くので参考にします。

すると、全ての回答が全文表示されます。

関連するQ&A