Currency exchange app

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
2












I'm making a currency exchange app, it works as follows:



User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.



  1. I need some help or guidance on how to setState in a cleaner way probably with a loop, map or a new function.

  2. Any help on how to create my CURRENCIES in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually.

  3. Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.

This is a working codesandbox.



import React, Component from "react";
import render from "react-dom";

import SingleInput from "./SingleInput.js";

import fetchedCurrencies from "./data.json";

const CURRENCIES = [
name: "MXN" ,
name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
];

class App extends Component
constructor(props)
super(props);
this.state =
MXN: "",
USD: "",
USDFix: "",
EUR: "",
CAD: "",
JPY: "",
GBP: ""
;


handleCurrencyInput = event =>
const name, value = event.target;
switch (name)
case "USD":
this.setState(
MXN: value * CURRENCIES[1].value,
USD: value,
USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
);
break;

case "USDFix":
this.setState(
MXN: value * CURRENCIES[2].value,
USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
USDFix: value,
EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
);
break;

case "EUR":
this.setState(
MXN: value * CURRENCIES[3].value,
USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
EUR: value,
CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
);
break;

case "CAD":
this.setState(
MXN: value * CURRENCIES[4].value,
USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
CAD: value,
JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
);
break;

case "JPY":
this.setState(
MXN: value * CURRENCIES[5].value,
USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
JPY: value,
GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
);
break;

case "GBP":
this.setState(
MXN: value * CURRENCIES[6].value,
USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
GBP: value
);
break;

default:
this.setState(
MXN: value,
USD: value / CURRENCIES[1].value,
USDFix: value / CURRENCIES[2].value,
EUR: value / CURRENCIES[3].value,
CAD: value / CURRENCIES[4].value,
JPY: value / CURRENCIES[5].value,
GBP: value / CURRENCIES[6].value
);

;

render()
return (
<div>
CURRENCIES.map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency.name
name=currency.name
placeholder=`$currency.name $`
value= ""
onChange=this.handleCurrencyInput
/>
))
</div>
);



export default App;






share|improve this question



























    up vote
    1
    down vote

    favorite
    2












    I'm making a currency exchange app, it works as follows:



    User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.



    1. I need some help or guidance on how to setState in a cleaner way probably with a loop, map or a new function.

    2. Any help on how to create my CURRENCIES in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually.

    3. Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.

    This is a working codesandbox.



    import React, Component from "react";
    import render from "react-dom";

    import SingleInput from "./SingleInput.js";

    import fetchedCurrencies from "./data.json";

    const CURRENCIES = [
    name: "MXN" ,
    name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
    name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
    name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
    name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
    name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
    name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
    ];

    class App extends Component
    constructor(props)
    super(props);
    this.state =
    MXN: "",
    USD: "",
    USDFix: "",
    EUR: "",
    CAD: "",
    JPY: "",
    GBP: ""
    ;


    handleCurrencyInput = event =>
    const name, value = event.target;
    switch (name)
    case "USD":
    this.setState(
    MXN: value * CURRENCIES[1].value,
    USD: value,
    USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
    EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
    CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
    JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
    GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
    );
    break;

    case "USDFix":
    this.setState(
    MXN: value * CURRENCIES[2].value,
    USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
    USDFix: value,
    EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
    CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
    JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
    GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
    );
    break;

    case "EUR":
    this.setState(
    MXN: value * CURRENCIES[3].value,
    USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
    USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
    EUR: value,
    CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
    JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
    GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
    );
    break;

    case "CAD":
    this.setState(
    MXN: value * CURRENCIES[4].value,
    USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
    USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
    EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
    CAD: value,
    JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
    GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
    );
    break;

    case "JPY":
    this.setState(
    MXN: value * CURRENCIES[5].value,
    USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
    USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
    EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
    CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
    JPY: value,
    GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
    );
    break;

    case "GBP":
    this.setState(
    MXN: value * CURRENCIES[6].value,
    USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
    USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
    EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
    CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
    JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
    GBP: value
    );
    break;

    default:
    this.setState(
    MXN: value,
    USD: value / CURRENCIES[1].value,
    USDFix: value / CURRENCIES[2].value,
    EUR: value / CURRENCIES[3].value,
    CAD: value / CURRENCIES[4].value,
    JPY: value / CURRENCIES[5].value,
    GBP: value / CURRENCIES[6].value
    );

    ;

    render()
    return (
    <div>
    CURRENCIES.map(currency => (
    <SingleInput
    type="text"
    maxLength=12
    key=currency.name
    name=currency.name
    placeholder=`$currency.name $`
    value= ""
    onChange=this.handleCurrencyInput
    />
    ))
    </div>
    );



    export default App;






    share|improve this question























      up vote
      1
      down vote

      favorite
      2









      up vote
      1
      down vote

      favorite
      2






      2





      I'm making a currency exchange app, it works as follows:



      User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.



      1. I need some help or guidance on how to setState in a cleaner way probably with a loop, map or a new function.

      2. Any help on how to create my CURRENCIES in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually.

      3. Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.

      This is a working codesandbox.



      import React, Component from "react";
      import render from "react-dom";

      import SingleInput from "./SingleInput.js";

      import fetchedCurrencies from "./data.json";

      const CURRENCIES = [
      name: "MXN" ,
      name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
      name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
      name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
      name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
      name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
      name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
      ];

      class App extends Component
      constructor(props)
      super(props);
      this.state =
      MXN: "",
      USD: "",
      USDFix: "",
      EUR: "",
      CAD: "",
      JPY: "",
      GBP: ""
      ;


      handleCurrencyInput = event =>
      const name, value = event.target;
      switch (name)
      case "USD":
      this.setState(
      MXN: value * CURRENCIES[1].value,
      USD: value,
      USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
      );
      break;

      case "USDFix":
      this.setState(
      MXN: value * CURRENCIES[2].value,
      USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
      USDFix: value,
      EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
      );
      break;

      case "EUR":
      this.setState(
      MXN: value * CURRENCIES[3].value,
      USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
      EUR: value,
      CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
      );
      break;

      case "CAD":
      this.setState(
      MXN: value * CURRENCIES[4].value,
      USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
      CAD: value,
      JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
      );
      break;

      case "JPY":
      this.setState(
      MXN: value * CURRENCIES[5].value,
      USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
      JPY: value,
      GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
      );
      break;

      case "GBP":
      this.setState(
      MXN: value * CURRENCIES[6].value,
      USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
      GBP: value
      );
      break;

      default:
      this.setState(
      MXN: value,
      USD: value / CURRENCIES[1].value,
      USDFix: value / CURRENCIES[2].value,
      EUR: value / CURRENCIES[3].value,
      CAD: value / CURRENCIES[4].value,
      JPY: value / CURRENCIES[5].value,
      GBP: value / CURRENCIES[6].value
      );

      ;

      render()
      return (
      <div>
      CURRENCIES.map(currency => (
      <SingleInput
      type="text"
      maxLength=12
      key=currency.name
      name=currency.name
      placeholder=`$currency.name $`
      value= ""
      onChange=this.handleCurrencyInput
      />
      ))
      </div>
      );



      export default App;






      share|improve this question













      I'm making a currency exchange app, it works as follows:



      User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.



      1. I need some help or guidance on how to setState in a cleaner way probably with a loop, map or a new function.

      2. Any help on how to create my CURRENCIES in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually.

      3. Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.

      This is a working codesandbox.



      import React, Component from "react";
      import render from "react-dom";

      import SingleInput from "./SingleInput.js";

      import fetchedCurrencies from "./data.json";

      const CURRENCIES = [
      name: "MXN" ,
      name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
      name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
      name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
      name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
      name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
      name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
      ];

      class App extends Component
      constructor(props)
      super(props);
      this.state =
      MXN: "",
      USD: "",
      USDFix: "",
      EUR: "",
      CAD: "",
      JPY: "",
      GBP: ""
      ;


      handleCurrencyInput = event =>
      const name, value = event.target;
      switch (name)
      case "USD":
      this.setState(
      MXN: value * CURRENCIES[1].value,
      USD: value,
      USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
      );
      break;

      case "USDFix":
      this.setState(
      MXN: value * CURRENCIES[2].value,
      USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
      USDFix: value,
      EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
      );
      break;

      case "EUR":
      this.setState(
      MXN: value * CURRENCIES[3].value,
      USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
      EUR: value,
      CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
      );
      break;

      case "CAD":
      this.setState(
      MXN: value * CURRENCIES[4].value,
      USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
      CAD: value,
      JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
      GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
      );
      break;

      case "JPY":
      this.setState(
      MXN: value * CURRENCIES[5].value,
      USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
      JPY: value,
      GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
      );
      break;

      case "GBP":
      this.setState(
      MXN: value * CURRENCIES[6].value,
      USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
      USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
      EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
      CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
      JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
      GBP: value
      );
      break;

      default:
      this.setState(
      MXN: value,
      USD: value / CURRENCIES[1].value,
      USDFix: value / CURRENCIES[2].value,
      EUR: value / CURRENCIES[3].value,
      CAD: value / CURRENCIES[4].value,
      JPY: value / CURRENCIES[5].value,
      GBP: value / CURRENCIES[6].value
      );

      ;

      render()
      return (
      <div>
      CURRENCIES.map(currency => (
      <SingleInput
      type="text"
      maxLength=12
      key=currency.name
      name=currency.name
      placeholder=`$currency.name $`
      value= ""
      onChange=this.handleCurrencyInput
      />
      ))
      </div>
      );



      export default App;








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 1 at 1:35









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Apr 17 at 4:38









      ricardoNava

      325




      325




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          In handleCurrencyInput the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;



          The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1



          So if you structure CURRENCIES as a dict instead of an array you can simplify to



          const CURRENCIES = 
          MXN: 1,
          USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
          USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
          EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
          CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
          JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
          GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
          ;

          ...

          handleCurrencyInput = event =>
          const name, value = event.target;
          const newState = ;
          for (const currency in CURRENCIES)
          this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))




          And adding additional currencies is trivial



          Adapted render:



          render() 
          return (
          <div>
          Object.keys(CURRENCIES).map(currency => (
          <SingleInput
          type="text"
          maxLength=12
          key=currency
          name=currency
          placeholder=
          value=
          onChange=this.handleCurrencyInput
          />
          ))
          </div>
          );






          share|improve this answer























          • Is a dict basically an object? or what is the difference?
            – ricardoNava
            May 3 at 2:28






          • 1




            Yes, just an simple object
            – generalhenry
            May 3 at 2:31










          • So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
            – ricardoNava
            May 3 at 3:50










          • I added an updated render, I'm not sure what decimal point issue you're encountering.
            – generalhenry
            May 3 at 3:55










          • So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
            – ricardoNava
            May 3 at 4:06











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192258%2fcurrency-exchange-app%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          In handleCurrencyInput the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;



          The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1



          So if you structure CURRENCIES as a dict instead of an array you can simplify to



          const CURRENCIES = 
          MXN: 1,
          USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
          USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
          EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
          CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
          JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
          GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
          ;

          ...

          handleCurrencyInput = event =>
          const name, value = event.target;
          const newState = ;
          for (const currency in CURRENCIES)
          this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))




          And adding additional currencies is trivial



          Adapted render:



          render() 
          return (
          <div>
          Object.keys(CURRENCIES).map(currency => (
          <SingleInput
          type="text"
          maxLength=12
          key=currency
          name=currency
          placeholder=
          value=
          onChange=this.handleCurrencyInput
          />
          ))
          </div>
          );






          share|improve this answer























          • Is a dict basically an object? or what is the difference?
            – ricardoNava
            May 3 at 2:28






          • 1




            Yes, just an simple object
            – generalhenry
            May 3 at 2:31










          • So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
            – ricardoNava
            May 3 at 3:50










          • I added an updated render, I'm not sure what decimal point issue you're encountering.
            – generalhenry
            May 3 at 3:55










          • So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
            – ricardoNava
            May 3 at 4:06















          up vote
          1
          down vote



          accepted










          In handleCurrencyInput the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;



          The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1



          So if you structure CURRENCIES as a dict instead of an array you can simplify to



          const CURRENCIES = 
          MXN: 1,
          USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
          USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
          EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
          CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
          JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
          GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
          ;

          ...

          handleCurrencyInput = event =>
          const name, value = event.target;
          const newState = ;
          for (const currency in CURRENCIES)
          this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))




          And adding additional currencies is trivial



          Adapted render:



          render() 
          return (
          <div>
          Object.keys(CURRENCIES).map(currency => (
          <SingleInput
          type="text"
          maxLength=12
          key=currency
          name=currency
          placeholder=
          value=
          onChange=this.handleCurrencyInput
          />
          ))
          </div>
          );






          share|improve this answer























          • Is a dict basically an object? or what is the difference?
            – ricardoNava
            May 3 at 2:28






          • 1




            Yes, just an simple object
            – generalhenry
            May 3 at 2:31










          • So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
            – ricardoNava
            May 3 at 3:50










          • I added an updated render, I'm not sure what decimal point issue you're encountering.
            – generalhenry
            May 3 at 3:55










          • So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
            – ricardoNava
            May 3 at 4:06













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          In handleCurrencyInput the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;



          The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1



          So if you structure CURRENCIES as a dict instead of an array you can simplify to



          const CURRENCIES = 
          MXN: 1,
          USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
          USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
          EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
          CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
          JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
          GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
          ;

          ...

          handleCurrencyInput = event =>
          const name, value = event.target;
          const newState = ;
          for (const currency in CURRENCIES)
          this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))




          And adding additional currencies is trivial



          Adapted render:



          render() 
          return (
          <div>
          Object.keys(CURRENCIES).map(currency => (
          <SingleInput
          type="text"
          maxLength=12
          key=currency
          name=currency
          placeholder=
          value=
          onChange=this.handleCurrencyInput
          />
          ))
          </div>
          );






          share|improve this answer















          In handleCurrencyInput the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;



          The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1



          So if you structure CURRENCIES as a dict instead of an array you can simplify to



          const CURRENCIES = 
          MXN: 1,
          USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
          USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
          EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
          CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
          JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
          GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
          ;

          ...

          handleCurrencyInput = event =>
          const name, value = event.target;
          const newState = ;
          for (const currency in CURRENCIES)
          this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))




          And adding additional currencies is trivial



          Adapted render:



          render() 
          return (
          <div>
          Object.keys(CURRENCIES).map(currency => (
          <SingleInput
          type="text"
          maxLength=12
          key=currency
          name=currency
          placeholder=
          value=
          onChange=this.handleCurrencyInput
          />
          ))
          </div>
          );







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 3 at 17:29









          ricardoNava

          325




          325











          answered May 1 at 2:33









          generalhenry

          24616




          24616











          • Is a dict basically an object? or what is the difference?
            – ricardoNava
            May 3 at 2:28






          • 1




            Yes, just an simple object
            – generalhenry
            May 3 at 2:31










          • So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
            – ricardoNava
            May 3 at 3:50










          • I added an updated render, I'm not sure what decimal point issue you're encountering.
            – generalhenry
            May 3 at 3:55










          • So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
            – ricardoNava
            May 3 at 4:06

















          • Is a dict basically an object? or what is the difference?
            – ricardoNava
            May 3 at 2:28






          • 1




            Yes, just an simple object
            – generalhenry
            May 3 at 2:31










          • So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
            – ricardoNava
            May 3 at 3:50










          • I added an updated render, I'm not sure what decimal point issue you're encountering.
            – generalhenry
            May 3 at 3:55










          • So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
            – ricardoNava
            May 3 at 4:06
















          Is a dict basically an object? or what is the difference?
          – ricardoNava
          May 3 at 2:28




          Is a dict basically an object? or what is the difference?
          – ricardoNava
          May 3 at 2:28




          1




          1




          Yes, just an simple object
          – generalhenry
          May 3 at 2:31




          Yes, just an simple object
          – generalhenry
          May 3 at 2:31












          So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
          – ricardoNava
          May 3 at 3:50




          So I encountered a problem with the way I'm using .map() on CURRENCIES if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map() but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
          – ricardoNava
          May 3 at 3:50












          I added an updated render, I'm not sure what decimal point issue you're encountering.
          – generalhenry
          May 3 at 3:55




          I added an updated render, I'm not sure what decimal point issue you're encountering.
          – generalhenry
          May 3 at 3:55












          So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
          – ricardoNava
          May 3 at 4:06





          So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
          – ricardoNava
          May 3 at 4:06













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192258%2fcurrency-exchange-app%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation