0

I have issues setting an inline condition for my shipping price :

cart.shippingPrice =
  country === ("France" || "United States") ? toPrice(20) : toPrice(10);

Only when I select France the condition returns 20, but I want United States to return it as well, is there a typo ?

2
  • 1
    ("France" || "United States" ) evaluates first, and just becomes "France". You need to check country === "France" || country === "United States Commented Dec 7, 2021 at 21:20
  • thank you, it works Commented Dec 7, 2021 at 21:21

2 Answers 2

2

You need to explicitly check against each country:

country === "France" || country === "United States"

If you "fear" that more countries will come to the list, you could create an array:

const countries = ["France", "United States"]

and then do

countries.includes(country)
Sign up to request clarification or add additional context in comments.

2 Comments

Ah nice suggestion of the array
this is very good, thank you !
0

In your expression and in question contents if it's returning 20 it means the condition is true.

Try:

cart.shippingPrice = (country === "France" || country === "United States) ? toPrice(20) : toPrice(10)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.