Gates
Gates

Reputation: 117

Coding Challenge debugging

So for the life of me I can't figure out why my function is not passing the test. I'm unable to replicate the error that the testing program is see in the below.

Here's the goal

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

Here's my implementation:

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;
}

Everything I've tested in the browser seems to be working fine:

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"

However when I give this function to the test script it produces errors on these commands:

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

I'm a fairly new programmer so I don't think its an issue with the unit test, so I'd value your all's input if there's something obvious I did wrong here.

-Thanks Ninja edit: fixed example

Upvotes: 1

Views: 1116

Answers (4)

Oreo
Oreo

Reputation: 555

You need to reset the capitalisation counter at the beginning of each word:

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.

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

It looks like you need to have every odd character capitalized, per word. So every word starts with a capital letter, and after that capitalization is toggled until the next word.

You could try to solve that by trying to subtract i from something, but I think by far the easiest way is to just reset the per-word counter when you encounter a space.

NB: I say 'odd' if you assume the first character has index 1, as I implemented it. If you are confused by this, you can write it to a 0-based version and capitalize 'even' characters. To do that, initialize charIndex to 0, reset it to -1 when you encounter a space, and use == 0 in the condition of the if statement. Same difference.

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

Upvotes: 0

Gates
Gates

Reputation: 117

Gave the fix to @Oreo for helping point out what was wrong. Here's is my own implementation for posterity:

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

Upvotes: 1

Stefan
Stefan

Reputation: 352

fixed it for you

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;
 }

edit: wait a second. Looks like the unit test is wrong

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

in the expected you have Up down Up down space Up (space is ignored) later on in the test 'A TeSt' , you have up space up (space is not ignored).

Upvotes: 0

Related Questions