2

I have made a compounding calculator function and I am unable to apply toFixed() to it. What I basically want to do is apply toFixed(2) in order to get just two digits after the decimal. Here comes the code thank you very much in advance.

const compound = function () {
  const value1 = document.getElementById("value1").value;
  const value2 = document.getElementById("value2").value;
  const value3 = document.getElementById("value3").value;
  const compResult = document.querySelector(".result").innerHTML =
    value1 * value2 ** value3;
  return compResult.toFixed(2);
};
const calcButton = document.querySelector(".calculate");
calcButton.addEventListener("click", () => {
  compound();
});

1 Answers1

2

You need to get the toFixed(2) value first and then display it on .result. The text of .result only changes when you assign its content explicitly.

const compound = function () {
    const value1 = document.getElementById("value1").value;
    const value2 = document.getElementById("value2").value;
    const value3 = document.getElementById("value3").value;
    const compResult = (value1 * value2 ** value3).toFixed(2); 
    document.querySelector(".result").innerText = compResult;
    return compResult;
};
Hao Wu
  • 17,573
  • 6
  • 28
  • 60