Skip to content

Instantly share code, notes, and snippets.

@kukiron
Last active February 22, 2018 19:34
Show Gist options
  • Select an option

  • Save kukiron/952df1e266ff318bcf4a7931ea18f3e6 to your computer and use it in GitHub Desktop.

Select an option

Save kukiron/952df1e266ff318bcf4a7931ea18f3e6 to your computer and use it in GitHub Desktop.
/** Question 1 **/
// Factory function to create the counter object
const myCounter = (count = 0) => ({
getState: () => count,
inc: () => ++count,
dec: () => --count
})
const counter = myCounter()
console.log(counter.getState()); // --> 0
counter.inc();
counter.inc();
console.log(counter.getState()); // --> 2
counter.inc();
console.log(counter.getState()); // --> 3
console.log(counter.inc()); // --> 4
console.log(counter.dec()); // --> 3
/** Question 2 **/
const url = "https://api.fixer.io/latest?base=EUR&symbols=USD"
const exchange = async (baseCurrency, targetCurrency) => {
const response = await fetch(url),
data = await response.json(),
{ base, rates } = data
baseCurrency === base && rates.hasOwnProperty(targetCurrency)
? console.log(rates[targetCurrency])
: console.log("No exchange rate for this currency")
}
// Returns the exchange rate (I believe that was required)
exchange("EUR", "USD") // --> 1.2276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment