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;i console.log(alternatingCase('Test');
压缩错误
结帐
function alternatingCase(字符串){
让newword=“”;
对于(i=0;i console.log(alternatingCase(“TesT”);
您只需将条件更新为if(string.charAt(i)==string.charAt(i).toUpperCase())
。您必须将大写字母与原始字母进行比较
const alternatingCase=函数(字符串){
让newword=“”;
对于(i=0;i
Hey查看这篇文章