Home > Net >  how to make letter "s" equals to letter "z" in input prompt in javascript in htm
how to make letter "s" equals to letter "z" in input prompt in javascript in htm

Time:02-03

i want to make the word brazil the same as brasil in this prompt

      let country1 = "Brazil",
        country2 = "Portugal";

      if (
        country.toLowerCase() === country1.toLowerCase() ||
        country.toLowerCase() === country2.toLowerCase()
      ) {
        alert("You speak Portugese.");
      } else {
        alert("You don't speak Portugese!");
      }

how can i make s and z equals?

the alert "You can speak Portugese" will be display if "brasil" or "brazil" will be inputted.

CodePudding user response:

You could use an array to check which countries belong to the language instead.

let country = prompt("What country are you from?").toLowerCase();
let portuguese = ['brazil','brasil','portugal']

if (portuguese.includes(country)) {
  alert("You speak Portuguese.");
} else {
  alert("You don't speak Portuguese!");
}

CodePudding user response:

This requirement is usually called “input sanitizing”. It means you take what you’ve got from the user and change it in such a way that it suits your needs. Upper-casing strings, removing excess whitespace, things like that.

In your case you could prepend a line country1 = country1.replace('razil', 'rasil'); and similar for country2 in front of the test. Then you only have to deal with a single spelling later.

  • Related