-2

我想为一组不断变化的特定字符循环一个数组。从那里我想切换字母的大小写,我被告知要使用这个特定的代码,但我无法让它工作。代码必须循环遍历字符数组,其中字符来自“输入”文本框。我怎样才能解决这个问题?

我应该提一下,我正在为高中上课做这个,所以我在编码方面还不够完美。


    <html> 
      <head> 
        <script type="text/javascript"> 
    function toggleCase() { 
        var i = document.getElementById("input").value.length;
        var word = document.getElementById("input").value;
        var chop =new array(i);
        for (a=i; a <= i; a++) {
            character[i] = word.slice(i-1,i)
            if (character[i] == character[i].toUpperCase;){
                character[i] = character[i].toLowerCase();
            }
            else {
                character[i] = character[i].toUpperCase();
            }
        }

        var final

        for (a=i; a <= i; a++) {
            final += character[i];
        }

        document.getElementById("output").value = final
    }
        </script> 
      </head> 
      <body> 
        <p>Enter letters for conversion:</p> 
        <form> 
          <input type="text" name="input" id="input" value="sample" maxlength="10"><br />
          <input type="text" name="output" id="output" value="" /> <br/>
          <input type="checkbox" name="toggle" value="ToggleCase" onClick="toggleCase(this.form)">Toggle Case<br/>
        </form> 
      </body> 
    </html> 
4

3 回答 3

0

也许你应该看看一些 api 和 howtos 但这是你的代码:

<html> 
<head> 
<script type="text/javascript"> 
function toggleCase() { 
  var text = document.getElementById("input").value;
  var character = new Array(text.length);

  for (i=0, a = text.length; i < a; i++) {
    character[i] = text[i];
    if (character[i] == character[i].toUpperCase){
      character[i] = character[i].toLowerCase();
    }
    else {
      character[i] = character[i].toUpperCase();
    }
  }

  document.getElementById("output").value = character.join('');
}
</script> 
</head> 
<body> 
<p>Enter letters for conversion:</p> 
<form> 
<input type="text" name="input" id="input" value="sample" maxlength="10"><br />
<input type="text" name="output" id="output" value="" /> <br/>
<input type="checkbox" name="toggle" value="ToggleCase" onClick="toggleCase()">Toggle Case<br/>
</form> 
</body> 
</html> 
于 2013-02-18T20:18:25.720 回答
0
function toggleCase() { 
   var str = document.getElementById("input").value;
   for (var i=0; i<str.length; i++) {
     str[i] = (str[i]==str[i].toUpperCase() ? str[i].toLowerCase() : str[i].toUpperCase());
   }
   document.getElementById("output").value = str;
}

这是一个完成这项工作的 for 循环。记住.toUpperCase.toLowerCase是函数

于 2013-02-18T20:22:21.377 回答
0

您可能想看看 String 的 split 方法。

var str = 'foo bar baz';

将字符串转换为 char 数组的最简单方法是将空字符串传递给 split 方法。

var charArray = str.split(''):
// charArray === ['f','o','o' ... 'b','a','z']; 

另外一个仅供参考,将空格字符传递给 split 会给你一个单词数组。

var wordArray = str.split(' ');
// wordArray === ['foo', 'bar', 'baz'];

我有点不清楚你必须解决什么,但看起来你想要一个函数将大写字母转换为小写字母,反之亦然。

var userInput = document.getElementById('someTextBox');
// If you want to be fancy you could use JQuery
// var userInput = $(#someTextBox').value() 

function toggledCase( str ) {

    var characters = str.split('');
    // The split method still uses iteration so should be able to say it 
    // satisfies the argument of looping through each character.  
    // Split just provides a good abstraction to interface with.

    var toggledCharacters = [];
    var i;
    var ch;
    for( i in characters ) {
        // For in loops on strings will return the indexes instead 
        // of the characters
        ch = characters[i];

        if( ch.toUpperCase() === ch ){
            toggledCharacters.push( ch.toLowerCase() );
        } else {
            toggledCharacters.push( ch.toUpperCase() );
        }

        // If you like one-liners, 
        // the conditional if statement could be replace with a ternay statement.

        //  toggledCharacters.push( ( ch.toUpperCase() === ch ) ? 
        //  ch.toLowerCase() : ch.toUpperCase();
    }

    return toggledCharacters;
}

我的 toggledCharacters 方法只返回一个字符数组,所以如果你想返回一个字符串,你可以创建一个 for 循环;

var arr = toggledCharacters('Foo'); // str = 'fOO';
var str = '';  

var i, ch;
for ( i in arr ) {
    str += arr[i]; // += is just a short hand notation of saying 
                   //      str = str + arr[i];
}

如果您很懒惰并且喜欢单行,请查看函数式编程。因为你还在上高中,所以有点超出范围。

var arr = toggledCharacters('Foo'); // str = 'fOO';
var str = arr.reduce( function(str, ch) {
    return str + ch;
} );

无论如何,这对我来说比老师概述的要干净得多。

function toggledCharacters(input) {
    input = input.split('');
    var output = [];

    var i, ch;
    for( i in input ) {
        output.push( ( input[i].toUpper() === input[i] ) ?
            input[i].toLower() : input[i].toUpper()
        );
    }

    return output.reduce( 
        function(str, ch) {
            return str + ch;
        }
    );
}

编辑:

哦,我只是注意到该代码中没有任何地方正在评估复选框的布尔值。

var checkBox = document.getElementByName('toggle');
var inputTextBox = document.getElementById('input'); 
var outputTextBox = document.getElementById('output');

var result = inputTextBox.value;

if( checkBox.checked ) {
    result = toggleCase( result );
}

outputTextBox.value = result;

哦,另一个仅供参考,因为您是初学者。确保您知道使用浏览器的控制台。

如果您使用的是 Firefox,请获取 firebug 应用程序。

Chrome,按 Ctrl-Shift-C。

IE 也有一个,我只是不想使用它。

与制作 html 演示页面并假设代码正常工作相比,控制台使使用 JS 进行实验变得更容易。

此外,这些开发人员工具可以向您展示对象的底层方法。它为学习 JS 提供了一种快速而有效的方式。

于 2013-02-18T21:06:51.373 回答