Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</body>
</html>
 
const countries = [{
        code: "en-US",
        currency: "USD",
        country: "United States"
    },
    {
        code: "en-NG",
        currency: "NGN",
        country: "Nigeria"
      }
]
function formatAsMoney(amount, buyerCountry) {
    let toCurrency = "";
    let isFound = false;
    countries.forEach(country => {
        if (country.code === buyerCountry) { //I am not sure if you `country.name`, change the key accordingly
            toCurrency = (amount).toLocaleString(country.code, { style: "currency", currency: country.currency });
            isFound = true;
        }
    });
    if (!isFound) {
        toCurrency = (amount).toLocaleString("en-US", { style: "currency", currency: "USD" }); //here `en-US` should be a string
    }
    return toCurrency;
}
console.log(formatAsMoney(2.15, "en-CA"))
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers