pelotador.1
pelotador.1

Reputation: 380

If/Else statement not working, giving undefined

I'm working on a project, and I need the English version of countries' names. I'm trying to have an if/else statement that will give the English spelling when the non-English spelling of a country is given, but it's not working. I would really appreciate any help or advice on why I am having this issue. Thank you!

let countryName;
  if (country === 'Shqipëria' || 'hqipëria') {
    let countryName ='Albania';
  }
  else if (country === 'Hayastán' || 'hayastán') {
   let countryName ='Armenia';
  }

 ...

  else if (country === 'Italia' || 'italia') {
   let countryName ='Italy';
  }

 ...

  else if (country === 'Türkiye' || 'türkiye') {
   let countryName = 'Turkey';
  }
 else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
   let countryName = 'Ukraine';
  }
 else {
    let countryName = country;
    }


  console.log(countryName)
  console.log(country)

console.log(country) gives Italia, but console.log(countryName) is undefined.

I have also tried doing this:

const [countryName, setCountryName] = useState('')
  if (country === 'Shqipëria' || 'hqipëria') {
    setCountryName('Albania');
  }
  else if (country === 'Hayastán' || 'hayastán') {
   setCountryName('Armenia');
  }

 ...

  else if (country === 'Italia' || 'italia') {
   setCountryName('Italy');
  }

 ...

  else if (country === 'Türkiye' || 'türkiye') {
   setCountryName('Turkey');
  }
 else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
   setCountryName('Ukraine');
  }
 else {
    setCountryName(country);
    }


  console.log(countryName)
  console.log(country)

But this was giving me an error stating Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

Upvotes: 0

Views: 128

Answers (2)

e.a.
e.a.

Reputation: 948

as mentioned in other answers your conditions only contain one equality check, so it wold always return true because a string's value by-itself is truthy, so you never get passed the first condition. for simplifying if statements, you can use the includes method. for example:

if (['Ukraїna','Україна','ukraїna','yкраїна'].includes(country))

and definitely declare country variable once & re-assign it in every condition.

let countryName;

if (...) {
  countryName = "x"
}

Upvotes: 1

amel
amel

Reputation: 1154

Cancerning your first code: countryname shouldn't be declared many times so your code should be like this:

let countryName;
if (country === 'Shqipëria' || country === 'hqipëria') {
    countryName ='Albania';
}
else if (country === 'Hayastán' || country === 'hayastán') {
    countryName ='Armenia';
}
else if (country === 'Italia' || country === 'italia') {
    countryName ='Italy';
}
else if (country === 'Türkiye' || country === 'türkiye') {
   countryName = 'Turkey';
}
 else if (country === 'Ukraїna' || country === 'Україна' || country === 'ukraїna' || country === 'yкраїна') {
   countryName = 'Ukraine';
}
 else {
    countryName = country;
}

console.log(countryName);
console.log(country);

Also, you should use

if(variable === 'value' || variable === 'value') 

instead of

if (variable === 'value' || 'value')

Upvotes: 0

Related Questions