Last active
February 22, 2018 19:34
-
-
Save kukiron/952df1e266ff318bcf4a7931ea18f3e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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