如何转换小写大写?(如果条件在不同的数字上。)

时间:2018-04-12 16:48:01

标签: javascript

嗨这是我第一次使用这个网站,我做了一些关于如何将小写字母转换为大写字母但仍然是filles的研究。要求是检查“偶数”是否将偶数位字母转换为不同类型(从下到上或从上到下)。下面是我的代码:

function question4(str,pos)
    {   var newLetter;
    var kkk=str;

        if (pos='even')
        {
            for (var i=0;i<str.length;i=i+2)
            {
                if (str[i].toString()==str[i].toString().toUpperCase())
                {
                    newLetter=str[i].toString().toLowerCase();
                    kkk[i]=newLetter;
                }else
                {
                    newLetter=str[i].toUpperCase();
                    kkk[i]=newLetter;
                }
            }
        }else if (pos='odd')
                    for ( i=0;i<str.length;i=i+2)
                    {
                        if (str[i]===str[i].toLowerCase())
                        {
                            alert('3');
                        }else if (str[i]===str[i].toUpperCase())
                        {
                            alert('4');
                        }
                    }
                    return kkk;
    }

要求是:编写一个函数,根据匹配pos参数函数值的位置,改变字符串中所有字符的大小写。函数(str,pos [even | odd])。示例(('abCd','odd')返回Abcd)

更新:现在我已经让“奇怪”状态正常工作,但“偶数”仍然无效,任何人都可以看看为什么?

function question4(strr,pos) {
var result  ;
var sum="";
var aaa;

for (var i = 0; i <= strr.length - 1; i = i + 1)
{
    if (pos == "odd"&&i%2==0)
    {   aaa=strr.charCodeAt(i);

        if (aaa >= 65 && aaa <= 90 )
        {
            result = String.fromCharCode(aaa + 32);
        } else
            result = String.fromCharCode(aaa - 32);
    }
    else if (pos == "even"&&i%2==1)
    {
        if (aaa >= 65 && aaa <= 90 )
        {
            result= String.fromCharCode(aaa + 32);
        } else
            result = String.fromCharCode(aaa - 32);
    }else result=strr[i];
    sum+=result;
}


return sum;

}

3 个答案:

答案 0 :(得分:0)

为实现此目的,您可以通过char:char:#/ p>来构造字符串

function question4(strInput, pos) {
  let str = ""; // The string to construct
  if (!pos || (pos !== "even" && pos !== "odd")) { // Validating pos
    throw "invalid pos";
  }
 for (var i=0;i<strInput.length;i++) // Looping on strInput
 {  
   let huPos = i + 1;
   if ((pos === "even" && huPos%2 == 1) ||
      (pos === "odd" && huPos%2 == 0)) { 
/* If we want switch odd and we are on even position or if we want switch even and we are on odd position, then we add the original char
*/
     str += strInput[i];
   }
   else {
     // In others case, we switch lower to upper and upper to lower
     let char = strInput[i];
     str += char == char.toUpperCase() ? char.toLowerCase() : char.toUpperCase();
   }
 }
 return str;
}
console.log(question4('abCdef', "odd")); // Return "AbcdEf"

Associated bin

编辑:

看到编辑后,我可以看到你想要在不使用toLower / UpperCase的情况下进行编辑。正如评论中所述,我认为这在js中是一个坏主意,但要进行实验,您可以实现这一点:

const reverser = {
  "a": "a".charCodeAt(0),
  "z": "z".charCodeAt(0),
  "A": "A".charCodeAt(0),
  "Z": "Z".charCodeAt(0),
};
const conversionValueToLower = reverser.a - reverser.A;
const conversionValueToUpper = reverser.A - reverser.a;
function reverseChar(char) {
  var code = char.charCodeAt(0);
    // If you want to go from upper to lower
    if (code >= reverser.A && code <= reverser.Z) {
       // Simply add the difference between lower and upper
      return String.fromCharCode(code + conversionValueToLower);
    } // Same logic here
    else if (code >= reverser.a && code <= reverser.z) {
      return String.fromCharCode(code + conversionValueToUpper);
    }
  /**
  Or use if you want full digit
  if (code <= 90 && code >= 65) {
    return String.fromCharCode(code + 32);
  }
  else if (code >= 97 && code <= 122) {
    return String.fromCharCode(code - 32);
  }
  **/
  return char; // Other case return original char
}

function question4(strInput, pos) {
  let str = "";
  if (!pos || (pos !== "even" && pos !== "odd")) {
    throw "invalid pos";
  }
 for (var i=0;i<strInput.length;i++)
 {  
   let huPos = i + 1;
   if ((pos === "even" && huPos%2 == 1) ||
      (pos === "odd" && huPos%2 == 0)) {
     str += strInput[i];
   }
   else {
     str += reverseChar(strInput[i]);
   }
 }
 return str;
}
console.log(question4('abCdef', "odd")); // return "AbcdEf"

Associated bin

另一种方法是编写模仿toLower / UpperCase

的utils函数

我在你的答案中也纠正了你的代码,而没有改变原始逻辑

function question4(strr,pos) {
  var result  ;
  var sum="";
  var aaa;

  for (var i = 0; i <= strr.length - 1; i = i + 1)
  {
    if (pos == "odd"&&i%2==0)
    {   aaa=strr.charCodeAt(i);

     if (aaa >= 65 && aaa <= 90 )
     {
       result = String.fromCharCode(aaa + 32);
     } else if(aaa >=97&&aaa <=122)
     { result = String.fromCharCode(aaa - 32);}
     else {result=strr[i];}
    }
    else if (pos == "even"&&i%2==1)
    {   aaa=strr.charCodeAt(i);
     if (aaa >= 65 && aaa <= 90 )
     {
       result= String.fromCharCode(aaa + 32);
     } else if(aaa >=97&&aaa <=122)
     { result = String.fromCharCode(aaa - 32);}
     else {result=strr[i];}
    }else {result=strr[i];}
    sum+=result;
  }


  return sum;
}

console.log(question4("abCd", "odd")) // return Abcd;

答案 1 :(得分:0)

这个问题的简单解决方案

&#13;
&#13;
// Function used to invert the letter case
const changeCase = c => {
  if (c === c.toUpperCase()) return c.toLowerCase()
  return c.toUpperCase()
}

const swapCaseConditional = (str, pos) => {
  // Use split method to convert string into array and map the array
  return str.split('').map((c, index) => {
    if (pos === 'even') {
      // if pos and index are even, change the letter case
      if (index % 2) return changeCase(c)
      return c
    }
    else {
      // if pos and index are odd, change the letter case
      if (!(index%2)) return changeCase(c)
      return c
    }
    // Convert to string
  }).join('')
}

console.log(swapCaseConditional('abCd', 'odd'))
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我工作了两个晚上,终于开始工作了。虽然没有完全涵盖所有情况,但几乎就在那里。

function question4(strr,pos) {
var result  ;
var sum="";
var aaa;

for (var i = 0; i <= strr.length - 1; i = i + 1)
{
    if (pos == "odd"&&i%2==0)
    {   aaa=strr.charCodeAt(i);

        if (aaa >= 65 && aaa <= 90 )
        {
            result = String.fromCharCode(aaa + 32);
        } else
            result = String.fromCharCode(aaa - 32);
    }
    else if (pos == "even"&&i%2==1)
    {   aaa=strr.charCodeAt(i);
        if (aaa >= 65 && aaa <= 90 )
        {
            result= String.fromCharCode(aaa + 32);
        } else if(aaa >=97&&aaa <=122)
        { result = String.fromCharCode(aaa - 32);}
        else {result=strr[i];}
    }else {result=strr[i];}
    sum+=result;
}


return sum;

}