I have no idea why my code doesn't work- Title Case a Sentence

Tell us what’s happening:

It works until var b on line 4 but I messed up the map function somehow. I don’t know where I messed up though. Please help!

Your code so far

function titleCase(str) {
  var c = str.toLowerCase();
  var b = c.split(" ");
  var d = b.map(function(i) {for(i=0; i<b.length; i++) {b.i[0].toUpperCase();}});
  
  var e = d.join("");

  return d;
 
  
}

titleCase("I'm a little tea pot");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

Hello there -
Here are a couple things:

your final lines would be more likely to be just return d.join(' '); you are currently setting a variable e to be the joined values (but looks like you might be joining them with an empty string, which would put no spaces in there.)

Your b.map is going a little wrong in a few ways.

  • b.map is going to iterate over the array of words that you’ve split. Each time, the i in function(i) will contain one of the words.
  • you are using i as your function argument, and also as your iteration loop variable, so that’s not good. In fact, it is not necessary to loop in the .map() callback function at all. You will have a word in i, you only need to uppercase the first letter, and then splice the remaining letters of the word to that first letter and return that. It will get stored into the array d
  • the you can do return d.join(' ');

It might be worthwhile for you to take your code and put it into PythonTutor and step through it, see what’s happening, make changes and incrementally fix the problems.

Good luck!

1 Like

You were doing really well up until the map function. You may need to look over the array map method on the MDN array.prototype.map. They explain it better than I can.

What you’re hoping is array in variable d looks like is something like [“I’m”, “A”, “Little” … but that’s not what you’re getting.

Map method will visit each element in the array (in your case an array individual words). Map uses your anonymous callback function to do something for each element and pushes the return value of that “does something” to a new array - in your case the variable named ‘d’.

So

  1. your map function does not need a for loop - looping through the array is built into map()
  2. you need a return statement in your map callback to push something onto array ‘d’
  3. the first parameter in callback function is the element so i = “i’m” then i = “a” …

Also you can’t change a string - you have to make a new string. If myWord = “lower” then myWord[0].toUpperCase() won’t change myWord to “Lower”. Since you clearly already understand .split() and .join() using that on each letter of word might be an option.

var d = b.map(function(aWord){
   var newWord = someTitleCaseMagicCode(aWord); // turn "i'm" into new string "I'm"
   return newWord;  //push titlecased word onto new array  - "I'm"
}):
1 Like

First, you need to understand how function parameters work, what return does, and that functions (when called) can be used as their returned value.

Consider the following:

var foo = function() {return 6};
var bar = function(input) {return input * 7};
var quux = bar(foo());
console.log(quux); //logs 42

Reason through it and internalize this behavior, because this behavior is fundamental to writing functional JavaScript.

Then, read up on map (MDN article linked to in @alhazen1’s post above).

Note particularly the section headed “Syntax”, especially the parameters and return value. The first parameter is a callback (a function to perform on each element of the array), which itself takes up to three parameters: currentValue, index, and array.

You can start by just using currentValue, then adding the others only if needed (hint: you probably won’t need them for this challenge). However, naming the currentValue parameter i suggests that you’re expecting an index, which is wrong (that would be the second parameter).

Also, take a look through a couple of the examples in the MDN article to help you understand more concretely what map does.