简体   繁体   English

如何在JavaScript中使用charAt检查值

[英]How to check whether a value using charAt in JavaScript

I am very new to JavaScript. 我对JavaScript非常陌生。 Presently, I am doing a Program where I want to translate a text into "rovarspracket". 目前,我正在做一个程序,希望将文本翻译成“ rova​​rspra​​cket”。 ie) Double every consonant and place an occurrence of "o" in between. 即)将每个辅音加倍,并在它们之间放置一个“ o”。 For ex: , translate("this is fun") should return the string "tothohisos isos fofunon" . 对于ex:,translation(“ this is fun”)应该返回字符串“ tothohisos isos fofunon”。 I am not able to get the result as desired. 我无法获得所需的结果。 Help me that i would learn. 帮我,我会学习。

This is my following code which i tried. 这是我尝试的以下代码。

<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> 

I've taken the liberty to simplify the functionality for you: 我已为您简化功能:

 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); 

Or, a little more compact: 或者,更紧凑一些:

 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); 

This is something you could do. 这是您可以做的。

 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> 

If you are specifically looking a solution with charAt function, here it is. 如果您特别在寻找具有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"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM