0

the question is there is a method such as .toUpperCase() which recognises that there might be spaces between the strings in different cases. I researched on it however couldn't find one. This is what I want to implement. It was one of the exercises on the code academy.

var movie = prompt("movie please").toUpperCase();
var getReview = function (movie) {
switch(movie){
    //case 'Toy Story 2':
        //console.log("Great Story. Mean prospector.");
        //break;
    //case 'Finding Nemo':
        console.log("Cool animation, and funny turtles.");
        break;
    case 'The Lion King':
        console.log("Great songs.");
        break;
    default:
        console.log("I don't know!");
}

};

I passed the exercise using a different code but I just wanted to ask out of curiousity.

Yuvi
  • 498
  • 8
  • 18

2 Answers2

2

You could remove all spaces:

movie = prompt("movie please").toUpperCase().replace(/\s/g,"");
switch(movie) {
    case "TOYSTORY2":
       // ....
    case "THELIONKING":
    case "LIONKING": // allow not having "the" as well
       // ....
}
Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
0

What about title case? see this answer

function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}
Community
  • 1
  • 1
rogelio
  • 1,521
  • 14
  • 19