I have been tasked with writing a function that follows specific instructions, but for some reason, it is not working as expected.
Here are the guidelines:
Utilize the .toLocaleString
function on both the amount and buyerCountry to format the amount into a currency with the currency symbol of the buyerCountry. The list of countries and their corresponding currency symbols can be found in the countries array provided in the starter code. If the buyerCountry is not listed in the countries array, default to United States and format the currency accordingly.
The code I have written does not seem to be compliant, and I am unsure why this is happening:
const countries = [
{
code: "US",
currency: "USD",
country: 'United States'
},
{
code: "NG”,
currency: "NGN”,
country: “Nigeria”
}
]
const formatAsMoney = (amount, buyerCountry) => {
let toCurrency = '';
countries.forEach(country => {
try {
if (value.country === buyerCountry) {
toCurrency = (amount).toLocalString(value.code, {
style: 'currency',
currency: value.currency
})
}
} catch (error) {
toCurrency = (amount).toLocalString(en-US, {
style: "currency",
currency: "USD"
});
}
});
return toCurrency;
}
Could you please help me identify the issue and suggest how to fix it? Thank you!