Search code examples
javascriptuppercaselowercase

Is there a way to use IgnoreCase in Javascript?


I want to transnlate some words, but i have problems when I write HOUSE in uppercase or mixing. it just work when i write in lowercase

<script>
var translate = prompt("Enter one of the next words:\nhouse\ntable\ndog\ncat");

switch (translate){
    case 'house': document.write("casa");
        break;
    case 'table': document.write("mesa");
        break
    case 'dog': document.write("perro");
        break;
    case 'cat': document.write("gato");
        break;
    default: document.write("Error");


}
</script>

Solution

  • Modified code. (As solution already given in comments. )

    <script>
    var translate = prompt("Enter one of the next words:\nhouse\ntable\ndog\ncat") || ''; // take '' if user cancel the prompt as it return `null`;
    translate  = translate.toLowerCase();
    switch (translate){
        case 'house': document.write("casa");
            break;
        case 'table': document.write("mesa");
            break
        case 'dog': document.write("perro");
            break;
        case 'cat': document.write("gato");
            break;
        default: document.write("Error");
    
    
    }
    </script>