更改字符串中的每个其他字母(JavaScript)?每个新单词从0开始

时间:2017-02-12 19:26:48

标签: javascript

我主要使用它,但是当某些字母彼此相邻时,它们并没有对它们做大写。任何想法为什么它会错过它们?我的代码和预期结果如下。问题在于“ooooo's”和“Looks”......

function toWeirdCase(string) {
  var splitStr = string.split(" ");
  var newArr = [];

  for (var j = 0; j < splitStr.length; j++) {
    var letters = splitStr[j].split("");
    for (var i = 0; i < letters.length; i++) {
      if (letters.indexOf(letters[i]) % 2 === 0) {
        letters[i] = letters[i].toUpperCase();
      }
    }
    newArr.push(letters.join(""));
  }
  console.log(newArr.join(" "));
}

console.log(toWeirdCase('ooooo')); //Should return 'OoOoO'
console.log(toWeirdCase('howdy')); //Should return 'HoWdY'
console.log(toWeirdCase('Looks')); //Should return 'LoOkS'
console.log(toWeirdCase('This')); //Should return 'ThIs'
console.log(toWeirdCase('is')); //Should return 'Is'
console.log(toWeirdCase('This is a test')); //Should return 'ThIs Is A TeSt'`

4 个答案:

答案 0 :(得分:2)

ES6解决方案。

&#13;
&#13;
function toWeirdCase(str){
  return str.split('').map((v,i) => i % 2 ? v.toLowerCase(): v.toUpperCase()).join('');
}

console.log(toWeirdCase('ooooo')); 
console.log(toWeirdCase('howdy')); 
console.log(toWeirdCase('Looks')); 
console.log(toWeirdCase('This')); 
console.log(toWeirdCase('is'));
console.log(toWeirdCase('This is a test'));
&#13;
&#13;
&#13;

您以前的解决方案。

&#13;
&#13;
function toWeirdCase(string) {
  var splitStr = string.split(" ");
  var newArr = [];

  for (var j = 0; j < splitStr.length; j++) {
    var letters = splitStr[j].split("");
    for (var i = 0; i < letters.length; i++) {
      if (i % 2 == 0) {
        letters[i] = letters[i].toUpperCase();
      } else {
        letters[i] = letters[i].toLowerCase();
      }
    }
    newArr.push(letters.join(""));
  }
  return newArr.join(" ");
}

console.log(toWeirdCase('ooooo')); 
console.log(toWeirdCase('howdy')); 
console.log(toWeirdCase('Looks')); 
console.log(toWeirdCase('This')); 
console.log(toWeirdCase('is'));
console.log(toWeirdCase('This is a test')); 
&#13;
&#13;
&#13;

修改:您问,为什么您的解决方案无效。它没有因为以下行:

if (letters.indexOf(letters[i]) % 2 === 0) {

如果,例如你有一个包含相同字母的单词,例如&#39; ooooo&#39;,条件letters.indexOf(letters[i]) % 2 === 0一直在返回0false),然后是if条件永远不会是真的 - 因此循环不会影响任何字母。

答案 1 :(得分:0)

善良的用户回答很棒,但这是另一个,适用于旧浏览器。

&#13;
&#13;
function toWeirdCase(string) {
    string = string.split("");
    for (var i = 0; i < string.length; i++) {
        if (string.hasOwnProperty(i) && i != 'length' && i % 2 == 0) {
            string[i] = string[i].toUpperCase();
        }
    }
    string = string.join("");
    console.log(string);
}

toWeirdCase('ooooo');
toWeirdCase('howdy');
toWeirdCase('Looks');
toWeirdCase('This');
toWeirdCase('is');
toWeirdCase('This is a test');
&#13;
&#13;
&#13;

答案 2 :(得分:0)

带循环的小函数:

function toWeirdCase(getString){
	var returned = '';
	for(var i=0;i<getString.length;i++){
		var method = i%2===0 ? 'toUpperCase':'toLowerCase';
		returned += getString[i][method]();
	}
	return returned;
}

console.log(toWeirdCase('Looks'));

答案 3 :(得分:0)

可能有助于在word =&gt;之间拆分逻辑。 WeirdCase转换和短语=&gt; WeirdCase转换:

function evenUpperCase(letter, i) {
  return i % 2 === 0 ? letter.toUpperCase() : letter.toLowerCase();
} 

function wordToWeirdCase(word) {
  return word.split('')
    .map(evenUpperCase)
    .join('');
}

function phraseToWeirdCase(phrase) {
  return phrase.split(' ')
    .map(wordToWeirdCase)
    .join(' ');
}

console.log(phraseToWeirdCase('ooooo')); //Should return 'OoOoO'
console.log(phraseToWeirdCase('howdy')); //Should return 'HoWdY'
console.log(phraseToWeirdCase('Looks')); //Should return 'LoOkS'
console.log(phraseToWeirdCase('This')); //Should return 'ThIs'
console.log(phraseToWeirdCase('is')); //Should return 'Is'
console.log(phraseToWeirdCase('This is a test'));