将数字转换为字母

时间:2012-08-17 17:49:49

标签: javascript

我正在尝试在其基本阶段制作一个javascript项目,其中脚本应该提示用户,将数字作为输入,将数字转换为字母并将其显示给用户。但是脚本不能正常工作。

<html>
<head>
<script type="text/javascript">
function num()
{
    var arr1=new Array();
    var arr2=new Array();
    var i;
    var start=0;
    var end=start + 1;
    arr1[0]=prompt('Enter Number');
    for (i=0;i<2;i++)
    {
        arr2[i]=arr1[0].slice(start, end);
        arr2[i]=parseInt(arr2[i]);
        start++;
        end=start + 1;
        switch(arr2[i])
        {
            case 1:
                document.body.innerHTML = document.body.innerHTML + 'one';
                break;
            case 2:
                document.body.innerHTML = document.body.innerHTML + 'two';
                break;
            case 3:
                document.body.innerHTML = document.body.innerHTML + 'three';
                break;
            case 4:
                document.body.innerHTML = document.body.innerHTML + 'four';
                break;
            case 5:
                document.body.innerHTML = document.body.innerHTML + 'five';
                break;
            case 6:
                document.body.innerHTML = document.body.innerHTML + 'six';
                break;
            case 7:
                document.body.innerHTML = document.body.innerHTML + 'seven';
                break;
            case 8:
                document.body.innerHTML = document.body.innerHTML + 'eight';
                break;
            case 9:
                document.body.innerHTML = document.body.innerHTML + 'nine';
                break;
        }
    }
}
onload=num();
</script>
</head>
<body>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

特别是,我真的不喜欢switch结构。我更喜欢JavaScript文字对象表示法。

请检查以下内容:

<强> HTML

<input type="button" id="getMyNumber" value="Get my Number"/>

JavaScript + jQuery

$(function() {
  $('#getMyNumber').on('click', function() {
      var numbers = {
          "1": "One",
          "2": "Two",
          "3": "Three",        
          "4": "Four",
          "5": "Five",
          "6": "Six",
          "7": "Seven",
          "8": "Eight",
          "9": "Nine",
          "10": "Ten",
          "default": "No number typed!"
      };
      var input = prompt("Type a number: ");                     
      alert(numbers[input] || numbers["default"]);
   });
});

如果您愿意,请点击jsFiddle。不需要转换或技巧,只需简单的JavaScript。

一些参考文献:

Mozilla - Working with Objects

The Case against Switch

- 编辑 -

以下是仅使用JavaScript的解决方案,以避免批评者(jQuery加载,重载等):

<强> HTML

<input type="button" id="getMyNumber" value="Get my Number" onclick="getNumberRepresentation()" />

仅限JavaScript

function getNumberRepresentation() {
  var numbers = {
      "1": "One",
      "2": "Two",
      "3": "Three",        
      "4": "Four",
      "5": "Five",
      "6": "Six",
      "7": "Seven",
      "8": "Eight",
      "9": "Nine",
      "10": "Ten",
      "default": "No number typed!"
  };
  var input = prompt("Type a number: ");                     
  alert(numbers[input]);
}

另一个jsFiddle版本。

答案 1 :(得分:0)

查看我为你制作的this小提琴。

编辑:来源:

HTML:

<p onclick="javascript:ask();">Click</p>​

使用Javascript:

function ask(){

    var inputAsString;
    var userInput = prompt("Pick a number");

    switch( parseInt(userInput) ){

        case 1:
            inputAsString = "One";
        break;
        case 2:
            inputAsString = "Two";
        break;
        default:
            inputAsString = "I don't know...";           
    }

    alert( "\"" + userInput + "\" as a string is: " + inputAsString );
}​