繁体   English   中英

编码挑战调试

[英]Coding Challenge debugging

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

这是目标

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编辑:修复示例

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

 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. 

给@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(' ');
}

为你修好了

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'中有向上向上向上空间向上(空间被忽略),你有空间向上(空间不被忽略)。

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

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

注意:如果您假设第一个字符的索引为1,我会说“奇怪”,因为我实现了它。 如果您对此感到困惑,可以将其写入基于0的版本并将“偶数”字符大写。 为此,将charIndex初始化为0,遇到空格时将其重置为-1,并在if语句的条件下使用== 0 相同的区别。

 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'); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM