编码挑战调试

时间:2017-08-22 15:33:05

标签: javascript

因此,对于我的生活,我无法弄清楚为什么我的功能没有通过测试。我无法复制测试程序在下面看到的错误。

这是目标

toWierdCase('test test testy') // 'TeSt TeSt TeStY'

这是我的实施:

function toWeirdCase(string){
  let out ="";
  let spaceCount = 0;

  for( let i=0; i<string.length; i++){
    if(string[i] == " "){
      spaceCount++;
    }
    if((i - spaceCount)%2 == 0) {
      out += string[i].toUpperCase();
    } else {
      out += string[i].toLowerCase();
    }
  }
  return out;
}

我在浏览器中测试的所有内容似乎都运行良好:

toWeirdCase("now is the time for all good cows to come to the aid of their pastures")

-> "NoW iS tHe TiMe FoR aLl GoOd CoWs To CoMe To ThE aId Of ThEiR pAsTuReS"

但是,当我将此函数提供给测试脚本时,它会对这些命令产生错误:

Expected: 'ThIs Is A TeSt', instead got: 'ThIs Is A tEsT'

我是一个相当新的程序员,所以我不认为它是单元测试的问题,所以如果有明显的东西,我会重视你的所有输入这里做错了。

-Thanks Ninja编辑:修复示例

4 个答案:

答案 0 :(得分:2)

您需要在每个单词的开头重置大小写计数器:

&#13;
&#13;
function toWeirdCase(string) {
  let out = "";      //New string array, to put our modified string into
  let wordStart = 0; //Integer to hold the index of starts of words
  
    for (let i = 0; i < string.length; i++) { //Loop over each input string character.
      if (string[i] == " ") { //If the current character is a space,
        wordStart = i + 1;    //we are about to start a new word!
      }
      
      if ((i - wordStart) % 2 == 0) {   //If we are an even distance away from the current wordStart,
        out += string[i].toUpperCase(); //save an UpperCase copy of the current character to "out".
      } else {                          //If not,
        out += string[i].toLowerCase(); //save a LowerCase copy of the current character to "out".
      }
    }
    
    return out; //Pass our resulting string to whoever called this function.
}

console.log(toWeirdCase("this is a test"));           //Test whether our function works,
console.log(toWeirdCase("AND THIS IS ANOTHER TEST")); //and cover our input bases.
&#13;
&#13;
&#13;

答案 1 :(得分:1)

向@Oreo提供修复,以帮助指出错误。这是我自己的后代实现:

function toWeirdCase(string){
  let runCase = (word) => {
    let out ="";
    for( let i=0; i<word.length; i++){
      if(i%2 == 0) {
        out += word[i].toUpperCase();
      } else {
        out += word[i].toLowerCase();
      }
    }
    return out;
  }
  let arr = string.split(' ');
  let outArr = [];
  for (let i=0; i< arr.length; i++) {
    outArr.push(runCase(arr[i]));
  }
  return outArr.join(' ');
}

答案 2 :(得分:0)

为你修好了

function toWeirdCase(string){
 let out ="";
 let spaceCount = 0;

for( let i=0; i<string.length; i++){

if(i%2 == 0) {
  out += string[i].toUpperCase();
} else {
  out += string[i].toLowerCase();
}
}
 return out;
 }

编辑: 等一等。看起来单元测试是错误的

预期:'这是一个TeSt',反而得到了:'这是一个很好的'

在预期你有向下向下空间向上(空间被忽略) 稍后在测试'A TeSt'中,你有空间(空间不被忽略)。

答案 3 :(得分:0)

看起来您需要将每个奇数字符大写, 每个字 。因此,每个单词都以大写字母开头,然后将大写字母切换到下一个单词。

您可以尝试通过尝试从某些内容中减去i来解决这个问题,但我认为到目前为止最简单的方法是在遇到空格时重置单字计数器。

注意:我说'奇怪'&#39;如果你假设第一个字符的索引为1,那么我就实现了它。如果您对此感到困惑,可以将其写入基于0的版本并大写“甚至”#39;字符。为此,请将charIndex初始化为0,遇到空格时将其重置为-1,并在== 0语句的条件下使用if。相同的差异。

&#13;
&#13;
function toWeirdCase(string){
  let out = "";
  let charIndex = 1; // Let's say the first character of each word has index 1.

  for( let i=0; i<string.length; i++){
    if(string[i] == " "){
      charIndex = 0; // The space before a word has index 0
    }
    if(charIndex++ %2 == 1) { // Every odd letter is capitalized. Note the post-read inc in here.
      out += string[i].toUpperCase();
    } else {
      out += string[i].toLowerCase();
    }
    
  }
  return out;
}

var input = 'this Is a tEST';
var expected = 'ThIs Is A TeSt';
var output = toWeirdCase(input);

console.log(output + ' should be ' + expected);
console.log(output == expected ? 'yay' : 'nay');
&#13;
&#13;
&#13;

相关问题