• ベストアンサー

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

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

  • reiman
  • お礼率73% (794/1076)

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

  • ベストアンサー
回答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

  • javascriptに関する質問です

    9×9のます目一つ一つに・を表示させるプログラムをつくっているのですがなかなかうまくいきませんどなたかご教授ください <script language="JavaScript" type="text/javascript"> <!-- document.write("<center>"); document.write("<h1>・の表</h1>"); document.write("<table border>"); var i; var j; for (i =・;i=<9 ){ document.write("<tr>"); for(j =・;j=<9 ){ document.write("<td>"); document.write(i * j) document.write("</td>") } document.write("</tr>"); } document.write("</table>"); //--> </script>

  • javascriptの九九の表の書き換えについて

    var a; for (a=1; a<10; a++) if (a<=1){ document.write("   "+a); }else{ document.write(" "+a);} document.write("<br>"); document.write("---------------------------------"); document.write("<br>"); var i, j; for (i=1; i<10; i++) { document.write(i+"|"+" "); for (j=1; j<10; j++) { var k = i*j; if ( k > 81 ) { break; } if (k>=10){ document.write(" "+k+" "); }else { document.write(" "+k);} } document.write("<br />"); } forを使った九九の表はできたのですが これをwhileを使ったコードに書き換えるにはどうしたらいいでしょうか?

  • リンク一覧の表示

    <SCRIPT language="JavaScript"> <!-- function ShowLinks(){ var msg = "リンクの一覧:<BR>"; for(i=0; i < document.links.length; i++){ msg = msg + "<A href=" + document.links[i] + ">" + document.links[i] + "</A><BR>"; } var new1 = window.open('','new1', 'toolbar=1, location=1, status=1, menubar=1, scrollbars=yes, resizable=1, width=650, height=800'); new1.document.write(msg); new1.document.close(); } //--> </SCRIPT> 下記のように表示されるのを リンクの一覧: http://***.ne.jp/bbns.cgi http://***.ne.jp/abc.cgi 下記のように番号を付けて表示したいのですが リンクの一覧: 0:http://***.ne.jp/bbns.cgi 1:http://***.ne.jp/abc.cgi

  • javascript 初心者です 変数について

    javascript 初心者です 変数について html内のbodyに次のように記述しています。 <script type="text/javascript"> <!-- var hensu1=7; var hensu2=2; var hensu3=5; var hensu4=4; for(i=1;i<=4;i++){ document.write("<div>"); for(j=1;j<="hensu"+i;j++){ document.write("<img src='sample.png'/>"); } document.write("</div>"); } --> </script> for(i=1;i<=4;i++)でdivを4つ並べ、 その中にそれぞれsample.pngをhensu分だけ並べ対のですが、 for(j=1;j<="hensu"+i;j++)の部分で "hensu"+i の記述の仕方が分からず、 上記の書き方だと表示されません。 正しい書き方を教えていただけないでしょうか?

  • 入れ子になったfor文の初期化式について

    javascriptを勉強中の初心者です。 以下のスクリプトを見てください。 document.write("<p>"); outloop: for (var i = 1; i < 5; i++){ for (var j = 1; j < 5; j++){ document.write(i + " × " + j + " = " + (i * j) + "<br />"); if (i * j > 10){ break outloop; } } } document.write("</p>"); 5未満の数同士の乗算の結果を順に表示し 乗算の結果が10を超えたら外のループを抜けるというスクリプトです。 for文がこのように入れ子になっている場合、 内側のループが二週になるとjはまた初期化されて1になるのでしょうか? 手持ちの参考書にあるfor文の説明では『初期化式は初めの一回だけ実行される』とあります。 内側のループが二回目以降の時、参考書のいう『初めの一回』になると考えていいのでしょうか。 初歩的な質問で恐縮ですが、ご存知の方どうか教えてください。 よろしくお願いいたします。

  • 処理のしくみがわかりません・・・

    以下のスクリプトはfor文による繰り返し処理の例です。 <script type="text/javascript"> <!-- var a="あ"; var b=new Array("い","う","え","お"); function FUNC1(){ for(i=0;i<b.length;i++){ a=a+b[i]; } document.write(a); } function FUNC2(){ for(i=0;i<b.length;i++){ document.write(a=a+b[i]); } } //--> </script> FUNC1の関数を実行すると、「あいうえお」、 FUNC2の関数を実行すると、「あいあいうあいうえあいうえお」 と表示されるのは理解できます。 それで試しにこのスクリプトの最後に、 FUNC1()+FUNC2(); を追加したところ、 「あいうえおあいあいうあいうえあいうえお」 と表示されると思いきや、 「あいうえおあいうえおいあいうえおいうあいうえおいうえあいうえおいうえお」 と表示されました。 どうもFUNC2を足す時、グローバル変数 a が "あ" ではなく、 "あいうえお" と代入されて実行されてるみたいです。 そして、FUNC1、FUNC2を実行すると、それぞれ 「あいうえおいうえおいうえお」 「あいうえおいうえおいあいうえおいうえおいうあいうえおいうえおいうえあいうえおいうえおいうえお」 と、理解不能の文字の並びになってしまいました。 なぜ FUNC1()+FUNC2(); を追加したらそれぞれの関数の実行値が変わってしまったのでしょうか? その処理の仕組みをご教授お願いいたします<(_ _)>

  • 3の倍数の合計値

    3の倍数を足した合計値を表示するにはどうすればいいか教えてください。 <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>for課題1</title> </head> <body> <script> var sum = 1; for (var i = 1; i <= 100; i++) { if (i % 3 === 0) { } sum += i; document.write(sum);} //表示がうまくいかない //1から100までの間で、3の倍数の数だけを足した合計値(1683)を表示したい </script> </body> </html>

  • java scriptのwrite()の結果

    <script> document.write("<table border>"); document.write("<tr>"); for(i=0;i<3;i++){ document.write("<td>"+i+"</td>"); } document.write("</tr>"); document.write("</table>"); </script> 例えば上記のような Java Script を作ったとします。 Internet Explorer で実行すると表(HTMLの結果?)が表示されますが、 そうでなく 以下のようなHTMLのスクリプトを表示させる事は できますか? もしできるなら方法をお願いします。 <table border><tr><td>0</td><td>1</td><td>2</td></tr></table>

  • for文の変化式について

    javascriptを勉強中の初心者です。 以下のスクリプトを見てください。 var num = 1; document.write("<p>"); for (var i = 0; i < 10; i++){ num *= 2; document.write("i = " + i + ",num = " + num + "<br />"); if (num >= 100){ break; } } document.write("<br />"); このスクリプトの実行結果は i=0 , num=2 i=1 , num=4 i=2 , num=8 i=3 , num=16 i=4 , num=32 i=5 , num=64 i=6 , num=128 なのですが、なぜ一行目の i の値が 0 なのでしょうか。 forの中の変化式(i++)で、初期化後の値 0 から 1 増えるので、1 だと思うのですが・・・ 1回目の処理では変化しない、等とは 参考書に書いてなかったので不思議に思いました。 ご存知の方、どうか理由を教えてください。 よろしくお願いいたします。

  • JavaScriptで九九

    繰り返し処理を用いて画像のように表示させたい場合はどこを修正すればいいですか? <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>九九</title> </head> <body> <script> for (var i = 1; i <= 9; i++) { for (var j = 1; j <= 9; j++) { document.write(`${i} * ${j} = ${i*j}<br/>`); } } </script> </body> </html>

専門家に質問してみよう