Basic Algorithm Scripting: Title Case a Sentence- console.log shows correct solution

Tell us what’s happening:
I’m not quite sure why the test isn’t passing since the console.log is showing the correct solution.

I’ve commented my code to the best of my beginner knowledge
Your code so far


//Title Case a Sentence
//return string with first letter of each word capitalized
function titleCase(str) {
let arrStr=str.toLowerCase().split(" "); //converts string str to lowercase letters
//splits str into array elements by the spaces between each word " "
str= ''; //empty string
//iterates through each element within the array
for (let i=0; i<arrStr.length; i++) {
  arrStr[i] = arrStr[i].charAt(0).toUpperCase() + arrStr[i].slice(1); //syntax:  string.charAt(index) returns the character at the specified index in a string. 
  //syntax string.toUpperCase() converts string characters to upper case
  //syntax string.slice(1) copies a given number of elements/characters into a new array/string. since only one parameter was given (1), it copies from 1st position to the end of the string  
  str += arrStr[i] + ' '; //str= str + arrStr[i] + ' '
  //combines array elements back into one string with a space bettween each words ' '
}
return str;
}
console.log(titleCase("I'm a little tea pot")); //logs I'm a Little Tea Pot
console.log(titleCase("sHoRt AnD sToUt")); //logs Short And Stout

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

Hi and welcome to the forum!

I believe that you have an extra space at the end of your string.

Yes, you have an extra space. And if you invert it to " " + arrStr[i] it would be in the beginning.

Option:

There is a method called join. That might be useful.

Wrote a simple analogous of the join method in case you want to play :smile:


function Errey(theArray){this.name=theArray}

Errey.prototype={
  constructor:Errey,
  myJoin(union = ' '){
  let result=""
  const length = this.name.length
  for(let i=0; i<length; i++){ i<length-1 ? 
     result += this.name[i] + union: 
     result+=this.name[i]
 }
  return result
  }}

let myArray = new Errey(["3","blue","1","brown"])
myArray.myJoin()
 //    returns  "3 blue 1 brown"

I just tested 1 example so it might have bugs

Hi Jeremy, thank you for the welcome and for pointing out the extra space! I wasn’t seeing it in the console but it makes sense

1 Like

Hi misterybodon! Thank you for the suggested method. -I’ll take a look and see if I can get it to work :slight_smile:

Welcome :slight_smile: . Try to console.log this to check your result:

console.log(titleCase("I'm a little tea pot").length,"I'm a little tea pot".length);

Got it to work with the join method. solution as follows:

function titleCase(str) {
  let arrStr=str.toLowerCase().split(" "); //converts string str to lowercase letters
//splits str into array elements by the spaces between each word " "
  //iterates through each element within the array
  for (let i=0; i<arrStr.length; i++) {
    arrStr[i] = arrStr[i].charAt(0).toUpperCase() + arrStr[i].slice(1); //syntax:  string.charAt(index) returns the character at the specified index in a string. 
    //syntax string.toUpperCase() converts string characters to upper case
    //syntax string.slice(1) copies a given number of elements/characters into a new array/string. since only one parameter was given (1), it copies from 1st position to the end of the string  
  }
  str= arrStr.join(" "); //syntax: array.join(separator). separator is a space between words " "
  // .join() method must be set to a variable 
  return str;
}
console.log(titleCase("I'm a little tea pot")); //logs I'm a Little Tea Pot

well done, time for the next exercise :smile: