我是JS的新手。我将参加一个JS训练营,我正在做一些课前练习。下面是一个coderbyte解决方案。请求是:
创建一个函数,在传递字符串时将交换字符串的大小写。
我的解决方案:
function swapCase(someStr){
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var upperAlphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var idxLower = [];
var idxUpper = [];
someArray = someStr.split("");
//Find out which indexes are lower case
for(var i = 0; i<someArray.length; i++){
for(var j = 0; j<alphabet.length; j++){
if(someArray[i] === alphabet[j]){
idxLower.push(i);
}
}
}
//find out which indexes are upper case
for(var k = 0; k<someArray.length; k++){
for(var m = 0; m<upperAlphabet.length; m++){
if(someArray[k] === upperAlphabet[m]){
idxUpper.push(k);
}
}
}
//convert lower cases to upper
for(var n = 0; n<idxLower.length; n++){
for(var p = 0; p<someArray.length; p++){
if(idxLower[n] === p){
someArray[p] = someArray[p].toUpperCase();
}
}
}
//convert upper cases to lower
for(var q = 0; q<idxUpper.length; q++){
for(var r = 0; r<someArray.length; r++){
if(idxUpper[q] === r){
someArray[r] = someArray[r].toLowerCase();
}
}
}
var newStr = someArray.join("");
console.log(newStr);
}
swapCase("Hello World Sup Bro My Name iS Richard!");
//returns hELLO wORLD sUP bRO mY nAME Is rICHARD!
我知道这是elogant的对立面。任何人都可以使用JS基础知识来帮助我理解如何以更简洁的方式进行此练习吗?
答案 0 :(得分:2)
听起来像:对于每一个角色,如果它是低位,我想把它变成上面,如果它是上面的话,我想让它变低。
这很容易:
function swapCase(str) {
var out = '';
for (var i=0; i<str.length; i++) {
// It's lower, or non-A-Z
if (str[i].toLowerCase() == str[i]) {
out += str[i].toUpperCase();
}
else {
out += str[i].toLowerCase();
}
}
return out;
}
alert(swapCase('Hello World Sup Bro My Name iS Richard!'));
(小提琴要证明这一点。](http://jsfiddle.net/rudiedirkx/m4mhsyuz/)
答案 1 :(得分:1)
这样可行。它是一个使用字符代码查看它是否已经大写的函数。这是UTF-8字符代码规范:http://www.w3schools.com/charsets/ref_utf_basic_latin.asp 65到90之间是大写,97到122之间是小写。
var reverseCase = function(str){
var output = '';
var code = '';
for(var i=0,len=str.length-1; i<=len; i++){
code = str.charCodeAt(i);
if(code >= 65 && code <= 90){
output+=str.charAt(i).toLowerCase();
}else if(code >=97 && code <= 122){
output+=str.charAt(i).toUpperCase();
}else{
output+=str.charAt(i);
}
}
return output;
}
reverseCase("Hello World Sup Bro My Name iS Richard!");
答案 2 :(得分:0)
与@ Moshe的答案基本相同,他比我提前几分钟......;)
function swapCase(someStr) {
var code_A = 'A'.charCodeAt(0);
var code_Z = 'Z'.charCodeAt(0);
var code_a = 'a'.charCodeAt(0);
var code_z = 'z'.charCodeAt(0);
var parsedStr = '';
for(i=0;i<someStr.length;i++) {
code = someStr.charCodeAt(i);
// If it's an uppercase
if (code >=code_A && code <= code_Z) {
code = code_a + code - code_A;
// Or if it is a lowecase
}else if (code >=code_a && code <= code_z) {
code = code_A + code - code_a;
}
parsedStr += String.fromCharCode(code);
}
return parsedStr;
}
swapCase('WaszUp Brah *7&9');
答案 3 :(得分:0)
如果只需要使用ASCII字符,则可以使用二进制操作。由于二进制运算符^
(也称为XOR作为开关),您可以切换第五位,即负责该情况的位:
function swapCaseASCII(str) {
var swappedCaseCharBuffer = new Array(str.length);
var fifthBit = 1 << 5; // or 32 if you prefer :)
for (var i = 0, l = str.length; i < l; i++) {
var charCode = str.charCodeAt(i);
// check if character belongs to a-z or A-Z first
if (charCode >= 65 && charCode <= 90 ||
charCode >= 97 && charCode <= 122) {
// here we can safely swap without generating an invisible char
charCode ^= fifthBit;
}
swappedCaseCharBuffer[i] = String.fromCharCode(charCode);
}
return swappedCaseCharBuffer.join('');
}