Title Case - Code works, but still getting red crosses for the challenge?

Title Case - Code works, but still getting red crosses for the challenge?
0.0 0


The below code runs ok in the FCC page, but when I run it here - https://repl.it/languages/javascript, there appears to be a backslash being inserted into my output wherever I have a single quote, i.e. where " I’m " is written… What is causing this?

var subjoin = "";
var finalcut = "";
function titleCase(str) {

  var splstr = str.toLowerCase().split(' ');
  // cap each letter and extract
  for (var i=0; i < splstr.length; i++) {
  var capChar = splstr[i].charAt(0).toUpperCase(); 
  var subarr = splstr[i].split('');
    // now replace the first index of each subarr with capChar
   var finalsub = subarr.splice(0,1,capChar); 
  // now join the subarr back together 
 subjoin = subjoin + " " + subarr.join('');
     finalcut = subjoin.slice(1);
return finalcut;}



Because that site is enclosing the string in single quotes

In Javascript, you can contain a string literal in either single quotes or double quotes, it doesn’t matter. As long as each string is surrounded by the same, it’s fine. They can be added with strings surrounded by the other, no problem. They are interchangeable, as long as each string segment starts and stops with the same, it’s fine.

The problem arises - what if you want a single or double quote inside the string. What if you wanted ‘It’s s’wonderful’ or “He said he likes “fast” women.” That causes a problem, You can’t have single quotes (apostrophes) inside a string surrounded by single quotes, and the same for double quotes. Javascript doesn’t know where the string is supposed to end.

You could switch it as “It’s s’wonderful” and ‘He said he likes “fast” women.’ You can have the other kind of quote inside, no problem.

The other option is an escape sequence. Certain special character are problematic to writer literally so we use a special code for them. We write a backslash followed by a code. For example, these strings could have been written like this: ‘It’s s’wonderful’ and “He said he likes “fast” women.” Yes, it looks weird, but when outputted on the web page with JS, it will look correct. This is the option that that web site chose.