Title Case a Sentence getting output,not pasing test cases

Tell us what’s happening:
I think I’m getting the correct output,yet the test cases are not passing.Please help.

Your code so far

function titleCase(str) {
var s=str.toLowerCase();
  var s1=s.split(" ");
  var r=[];
  for(var i=0;i<s1.length;i++){
  for(var j=0;j<s1[i].length;j++){
     if(j===0){r.push(s1[i][j].toUpperCase()+" ");}
     else{ r.push(s1[i][j]+" ");}
   else if(j===0){
  return r.join("");

titleCase("sHoRt AnD sToUt");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:

You are probably adding a space at the end of your string.

In that case, r.pop() should return a space but is returning a ‘t’ which means ‘t’ is the last character in the array. Please correct me if I’m wrong.

The pop() is actually returning "t ". The last element in the array is a string ending with a space.

Edit: repl.it embedding doesn’t seem to be working right. See it here.

1 Like

thanks for helping. :slight_smile: