Freecodecamp: It works but will not let me past

Created on 27 Jul 2016  路  2Comments  路  Source: freeCodeCamp/freeCodeCamp

Title Case a Sentence (challenge)

https://www.freecodecamp.com/challenges/title-case-a-sentence#?solution=%0A%0Afunction%20titleCase(str)%20%7B%0A%20%20var%20arryStr%3Dstr.split('%20')%3B%0A%20%20str%3D%22%22%3B%0A%20%20for%20(i%3D0%3B%20i%3CarryStr.length%3Bi%2B%2B)%7B%0A%20%20%20%20var%20temp%20%3DarryStr%5Bi%5D.charAt().toUpperCase()%3B%0A%20%20%20%20var%20lower%3D%22%22%3B%0A%20%20%20%20%20for(j%3D1%3Bj%3CarryStr%5Bi%5D.length%3B%20j%2B%2B)%7B%0A%20%20%20%20%20%20%20lower%2B%3DarryStr%5Bi%5D.charAt(j).toLowerCase()%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%7D%0A%20%20%20%20str%2B%3Dtemp%2Blower%2B%22%20%22%3B%0A%20%20%7D%0A%20%20%0A%20%20return%20str%3B%0A%7D%0A%0AtitleCase(%22I'm%20a%20little%20tea%20pot%22)%3B%0A

Issue Description

the program I wrote works fine but it will not let me pass

Browser Information

  • Browser Name, Version: chrome Version 51.0.2704.103 (64-bit)
  • Operating System: OSX 10.11.15
  • Mobile, Desktop, or Tablet: Desktop

Your Code

function titleCase(str) {
  var arryStr=str.split(' ');
  str="";
  for (i=0; i<arryStr.length;i++){
    var temp =arryStr[i].charAt().toUpperCase();
    var lower="";
     for(j=1;j<arryStr[i].length; j++){
       lower+=arryStr[i].charAt(j).toLowerCase();

     }
    str+=temp+lower+" ";
  }

  return str;
}

Screenshot

screen shot 2016-07-26 at 8 44 14 pm

Most helpful comment

I think the line str+=temp+lower+" "; add a whitespace to the end of the text when in the last iteration which makes the strings not match. Hint: Have a look at Array.prototype.join() to avoid the problem.

All 2 comments

I think the line str+=temp+lower+" "; add a whitespace to the end of the text when in the last iteration which makes the strings not match. Hint: Have a look at Array.prototype.join() to avoid the problem.

@cjoshmartin as @pnahratow suggests, double check the spaces you add when you join strings. If you get stuck or have questions with a challenge in the future, please first ask questions to the Help Room. Happy coding!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ar5had picture ar5had  路  3Comments

jurijuri picture jurijuri  路  3Comments

DaphnisM picture DaphnisM  路  3Comments

robwelan picture robwelan  路  3Comments

raisedadead picture raisedadead  路  3Comments