【问题标题】:How to check whether a value using charAt in JavaScript如何在 JavaScript 中使用 charAt 检查一个值
【发布时间】:2016-10-26 07:19:37
【问题描述】:

我对 JavaScript 很陌生。目前,我正在做一个程序,我想将文本翻译成“rovarspra​​cket”。即)将每个辅音加倍,并在其间放置一个“o”。例如: , translate("this is fun") 应该返回字符串 "tothohisos isos fofunon" 。我无法得到想要的结果。帮助我学习。

这是我尝试过的以下代码。

<button type="button" onclick="translate('de')">try it</button>
    <h2 id="ad" />
<script>
            function translate(t)
                {//alert(t.length);exit;
                    var l=t.length;
                    var v=["a","e","i","o","u",""];
                    var b="";
                    for(var i=0;i<l;i++)
                        { //alert(i);exit;
                            var c=t.charAt[i];alert(c);
                            if(v.indexOf(c)!=-1)
                            {   
                                b=(b+(c));
                            }
                            else
                            {
                                b=(b+(c="o"+c));
                            }
                        }

                    document.getElementById("ad").innerHTML=b;
                }

        </script> 

【问题讨论】:

    标签: javascript charat


    【解决方案1】:

    我冒昧地为您简化了功能:

    var str = "this is fun",
        arr = str.split(''); // Split the string into an array of separate characters.
        mapped = arr.map(function(c){
            if(["a","e","i","o","u"," "].indexOf(c) == -1) // If the character is a consonant
                return c + 'o' + c; // replace it,
            return c;               // otherwise, just keep the current character.
        }),
        result = mapped.join('');   // Rebuild the string
    
    console.log(result);

    或者,更紧凑一点:

    var str = "this is fun",
        result = str.split('').map(function(c){
            return (["a","e","i","o","u"," "].indexOf(c) == -1) ? (c + 'o' + c) : c;
        }).join('');
    
    console.log(result);

    【讨论】:

    • 这对我帮助很大。我们可以将它与 CharAt.. 一起使用吗?无论如何,这两个答案都对我有用
    • 这个逻辑在我看来不正确。而不是“tothohisos isos fofunon”,您的解决方案在单词之间返回“tothohisos o isos o fofunon”,而“o”被空格包围。
    • @Sreekanth:我忘记了空格。该解决方案现在有效。
    • @DevaAsirvatham: charAt 不适用于这种方法。
    • 所有反对票是怎么回事?这段代码有效,比 OP 的代码短,有解释,答案也接受了……怎么了?
    【解决方案2】:

    这是你可以做的。

    function translate(input) {
      var outputString = [];
      var vowels = ['a', 'e', 'i', 'o', 'u'];
      input.split("").forEach(function(charValue) {
    
        if (vowels.includes(charValue) || charValue == ' ') {
          outputString.push(charValue);
        } else {
          outputString.push(charValue + 'o' + charValue);
        }
      });
    
      document.querySelector("#output").innerHTML = outputString.join("");
    }
    div {
      padding: 10px;
    }
    <button type="button" onclick="window.translate('this is fun')">try it</button>
    <div id="output"></div>

    如果您正在寻找具有 charAt 函数的解决方案,这里就是。

    function translate(input) {
    
      var outputString = [],
        vowels = ['a', 'e', 'i', 'o', 'u'];
    
      for (var idx = 0; idx < input.length; idx++) {
        var currentChar = input.charAt(idx);
        if (vowels.indexOf(currentChar) === -1 && currentChar != " ") {
          outputString.push(currentChar + "o" + currentChar);
        } else {
          outputString.push(currentChar);
        }
      }
      console.log(outputString.join(""));
    }
    translate("this is fun");

    【讨论】:

    • 当我运行代码 sn-p 时,它在 .如何解决它
    • 是吗?运行时我没有看到错误。你能再检查一下吗?
    • 只能通过第二个选项得到答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多