Output as excepted, yet not getting through

The exercise under JAVASCRIPT called " Basic Algorithm Scripting: Title Case a Sentence". The code is down below:

function titleCase(str) {
  var j, k=''; 
  var n = str.split(' ')
  var m = n.map((N) => {
     j =  N.charAt(0).toUpperCase() + N.slice(1).toLowerCase()
     k = k + " " + j
  })
  console.log(k.trim())
  
 // return str;
}

titleCase("sHoRt AnD sToUt");

Output: Short And Stout

Note: Any help would be really appreciated.

if that is your whole code as you are submitting, you do not have a return statement, your function just returns undefined

what’s the challenge link?

next time please use the Ask for Help button to have a precompiled post with your code correctly
formatted and the challenge link - you can still write your questions before submitting


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you so much. Yes, I had to return it instead of consoling it out. Even though the code works just fine with the consoling, the question asked for a Return.
Thank you for your time.

  • You should be using forEach, not map. Unless you are going to use the returned array there is no point in using map.

  • You should really try to use more meaningful variable names.

  • The code inside the function works, the function itself doesn’t really work. A function is supposed to produce an output or some side-effect. Yes, logging is a side-effect but a pretty useless one unless you are writing a console application. Take the trim method you are using, how useful would it be if it just printed the trimmed string to the console but didn’t give you a new string to use?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.