0

つまり、これは数字のお金を単語に変換する単純なJavaScriptコードです。問題は、ブラウザがウィンドウにプロンプ​​トを表示しないことです。コードを何度も読みましたが、エラーを見つけることができません。誰か助けてもらえますか?

<script type="text/javascript">
        document.writeln("hello World");
    var output=new Array("Rs.",null,null,null,null,null,null,null,null,null,null,null,null,null,null);

        str=window.prompt("Enter a number:","0");
        n1=parseFloat(str);
        if(isNaN(n1))
        {
         docucument.writeln("Enter a Valid number");
         return;
        }
        else
        {
            paise=(n1%1)*100;
            rupee=parseInt(str);
            output[14]="paise only";

            output[13]=two_digit(paise);

            output[12]="and";
            x=rupee%100;
            output[11]=two_digit(x);
            rupee=rupee/100;
            //if(rupee<=0) break end;

            output[10]="hundred";
            x=rupee%10;
            output[9]=one_digit(x);
            rupee=rupee/10;
            //if(rupee<=0) break end;

            output[8]="thousand";
            x=rupee%100;
            output[7]=two_digit(x);
            rupee=rupee/100;
            //if(rupee<=0) break end;

            output[6]="lakhs";
            x=rupee%100;
            output[5]=two_digit(x);
            rupee=rupee/100;
            //if(rupee<=0) break end;

            output[4]="crore";
            x=rupee%100;
            output[3]=two_digit(x);
            rupee=rupee/100;
            //if(rupee<=0) break end;

            output[2]="hundred";
            x=rupee%10;
            output[1]=one_digit(x);
            rupee=rupee/10;
            //if(rupee<=0) break end;

            //end:

            document.writeln(output.join(" "));

        }

        function two_digit(n)
        {
         if(n>=11 && n<=19)
         {
          x=eleven(n);
          return x;
         }
         else
         {
          a=n%10;
          y=one_digit(a);
          a=n/10;
          switch(a)
          {
                    case 1: x="ten"; 
                           break;
                    case 2: x="twenty"; 
                           break;
                    case 3: x="thirty"; 
                           break;   
                    case 4: x="forty"; 
                           break;
                    case 5: x="fifty"; 
                           break;
                    case 6: x="sisty"; 
                           break;
                    case 7: x="seventy"; 
                           break;
                    case 8: x="eighty"; 
                           break;
                    case 9: x="ninty"; 
                           break;
                    case 0: x=null; 
                           break;                                  
         }
         return (x+y);
        }
        function one_digit(n)
        {
         switch(n)
         {
                    case 1: x="one"; 
                           break;
                    case 2: x="two"; 
                           break;
                    case 3: x="three"; 
                           break;   
                    case 4: x="four"; 
                           break;
                    case 5: x="five"; 
                           break;
                    case 6: x="six"; 
                           break;
                    case 7: x="seven"; 
                           break;
                    case 8: x="eigh"; 
                           break;
                    case 9: x="nine"; 
                           break;
                    case 0: x=null; 
                           break;                                  
         }
         return (x);
        }
        function eleven(n)
        {
         switch(n)
         {
                    case 11: x="eleven"; 
                           break;
                    case 12: x="twelve"; 
                           break;
                    case 13: x="thirteen"; 
                           break;   
                    case 14: x="fourteen"; 
                           break;
                    case 15: x="fifteen"; 
                           break;
                    case 16: x="sixteen"; 
                           break;
                    case 17: x="seventeen"; 
                           break;
                    case 18: x="eighteen"; 
                           break;
                    case 19: x="nineteen"; 
                           break;

         }
         return (x);
        }
</script>
4

3 に答える 3

2

else {関数内のを取り除きtwo_digit()ます。角かっこがありません(そして、else常に前に戻るので、コードでは必要ありませんif)。

returnまた、コードの最初のものを取り除きます。関数の外部でAreturnを使用することはできません(すばやく「間違った」方法で終了する場合は使用できますthrow 'End';が、実際にはエラーにのみ使用されることになっています)。

于 2012-08-20T05:25:48.203 に答える
1

私が見つけた2つの構文エラーがあります:
1。return関数の外にステートメントがあり
ます。2。終了がありません。}

提案:画面に印刷するテストに
改行を追加する必要があります。<br>

次の修正されたコードは私のために働きます:

    <script type="text/javascript">
            document.writeln("hello World");
            var output=new Array("Rs.",null,null,null,null,null,null,null,null,null,null,null,null,null,null);

            str=window.prompt("Enter a number:","0");
            n1=parseFloat(str);
            if(isNaN(n1))
            {
             docucument.writeln("Enter a Valid number");
            }
            else
            {
                paise=(n1%1)*100;
                rupee=parseInt(str);
                output[14]="paise only";

                output[13]=two_digit(paise);

                output[12]="and";
                x=rupee%100;
                output[11]=two_digit(x);
                rupee=rupee/100;
                //if(rupee<=0) break end;

                output[10]="hundred";
                x=rupee%10;
                output[9]=one_digit(x);
                rupee=rupee/10;
                //if(rupee<=0) break end;

                output[8]="thousand";
                x=rupee%100;
                output[7]=two_digit(x);
                rupee=rupee/100;
                //if(rupee<=0) break end;
                output[6]="lakhs";
                x=rupee%100;
                output[5]=two_digit(x);
                rupee=rupee/100;
                //if(rupee<=0) break end;

                output[4]="crore";
                x=rupee%100;
                output[3]=two_digit(x);
                rupee=rupee/100;
                //if(rupee<=0) break end;

                output[2]="hundred";
                x=rupee%10;
                output[1]=one_digit(x);
                rupee=rupee/10;
                //if(rupee<=0) break end;

                //end:

                document.writeln(output.join(" "));

            }

            function two_digit(n)
            {
             if(n>=11 && n<=19)
             {
              x=eleven(n);
              return x;
             }
             else
             {
              a=n%10;
              y=one_digit(a);
              a=n/10;
              switch(a)
              {
                        case 1: x="ten";
                               break;
                        case 2: x="twenty";
                               break;
                        case 3: x="thirty";
                               break;
                        case 4: x="forty";
                               break;
                        case 5: x="fifty";
                               break;
                        case 6: x="sisty";
                               break;
                        case 7: x="seventy";
                               break;
                        case 8: x="eighty";
                               break;
                        case 9: x="ninty";
                               break;
                        case 0: x=null;
                               break;
             }
             return (x+y);
            }
}
            function one_digit(n)
            {
             switch(n)
             {
                        case 1: x="one";
                               break;
                        case 2: x="two";
                               break;
                        case 3: x="three";
                               break;
                        case 4: x="four";
                               break;
                        case 5: x="five";
                               break;
                        case 6: x="six";
                               break;
                        case 7: x="seven";
                               break;
                        case 8: x="eigh";
                               break;
                        case 9: x="nine";
                               break;
                        case 0: x=null;
                               break;
             }
             return (x);
            }
            function eleven(n)
            {
             switch(n)
             {
                        case 11: x="eleven";
                               break;
                        case 12: x="twelve";
                               break;
                        case 13: x="thirteen";
                               break;
                        case 14: x="fourteen";
                               break;
                        case 15: x="fifteen";
                               break;
                        case 16: x="sixteen";
                               break;
                        case 17: x="seventeen";
                               break;
                        case 18: x="eighteen";
                               break;
                        case 19: x="nineteen";
                               break;

             }
             return (x);
            }    
    </script>
于 2012-08-20T05:36:04.327 に答える
0

これが私の見解です

デモ

window.onload=function() {
    document.getElementById("form1").onsubmit=function() {
        return convert(this);
    }
}
var output=["Rs.",null,null,null,null,null,null,null,null,null,null,null,null,null,null];

function convert(theForm) {
  var paise,rupee,x,
    str=theForm.num.value,
    n1=parseFloat(str);
  if (isNaN(n1)) {
    document.getElementById("out").innerHTML="Enter a Valid number";
    return false;
  }
  paise=(n1%1)*100;
  rupee=parseInt(n1);
  output[14]="paise only";
  output[13]=two_digit(paise);
  output[12]="and";
  x=rupee%100;
  output[11]=two_digit(x);
  rupee=parseInt(rupee/100);
  //if(rupee<=0) break end;
  output[10]="hundred";
  x=rupee%10;
  output[9]=num2text(x);
  rupee=parseInt(rupee/10);
  //if(rupee<=0) break end;
  output[8]="thousand";
  x=rupee%100;
  output[7]=two_digit(x);
  rupee=parseInt(rupee/100);
  //if(rupee<=0) break end;
  output[6]="lakhs";
  x=rupee%100;
  output[5]=two_digit(x);
  rupee=parseInt(rupee/100);
  //if(rupee<=0) break end;
  output[4]="crore";
  x=rupee%100;
  output[3]=two_digit(x);
  rupee=parseInt(rupee/100);
  //if(rupee<=0) break end;
  output[2]="hundred";
  x=rupee%10;
  output[1]=num2text(x);
  rupee=parseInt(rupee/10);
  //if(rupee<=0) break end;
  document.getElementById("out").innerHTML=output.join(" ");
  return false;
}
function two_digit(n) {
  if(n>=11 && n<=19) {
    return num2text(n);
  }
  var a=n%10;
  var y=num2text(a);
  a=parseInt(n/10);
  x = ["","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninty"][a];
  return (x+y);
}
function num2text(n) {
  return ["","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
  "sixteen","seventeen","eighteen","nineteen"][n];
}
于 2012-08-20T05:49:32.033 に答える