Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回相反字母的函数_Javascript - Fatal编程技术网

Javascript 返回相反字母的函数

Javascript 返回相反字母的函数,javascript,Javascript,我想编写一个返回交替字符的函数,例如: "hello world".toAlternatingCase() === "HELLO WORLD" "HELLO WORLD".toAlternatingCase() === "hello world" "HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld" 这是我的尝试 const alternatingCase = function (string) { let newword = "";

我想编写一个返回交替字符的函数,例如:

"hello world".toAlternatingCase() === "HELLO WORLD"
"HELLO WORLD".toAlternatingCase() === "hello world"
"HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
这是我的尝试

const alternatingCase = function (string) {
  let newword = "";

  for (i = 0; i<string.length; i++) {
     if (string.charAt(i).toUpperCase())
    {
    newword+= string.charAt(i).toLowerCase()
    }

   else 
   {
   newword+=string.charAt(i).toUpperCase()
   }
  }
  return newword  
}

const alternatingCase=函数(字符串){
让newword=“”;

对于(i=0;i您需要检查字符是否为大写,然后取小写

不要忘记声明所有变量

这种方法使用一个函数,其中一个条件用于返回两个不同的表达式

const alternatingCase=函数(字符串){
让newword=“”;
for(设i=0;iconsole.log(alternatingCase('Test');
压缩错误

结帐

function alternatingCase(字符串){
让newword=“”;
对于(i=0;iconsole.log(alternatingCase(“TesT”);
您只需将条件更新为
if(string.charAt(i)==string.charAt(i).toUpperCase())
。您必须将
大写字母与
原始字母进行比较

const alternatingCase=函数(字符串){
让newword=“”;
对于(i=0;i
Hey查看这篇文章