Home > database >  Adding two outputs together
Adding two outputs together

Time:07-20

I got a profit calculator from a developer, which works perfectly, and I want to add the final result with the amount which has been typed in.

This is my code:

//////////PROFIT CALCULATOR

const numbers = document.querySelectorAll(".number");
const resultText = document.querySelector(".result");
let value, percent; // must be declared outside of the foreach

numbers.forEach((number) => {
  number.addEventListener("input", () => {
    if (number.classList.contains("value")) {
      value = parseFloat(number.value);
    }

    if (number.classList.contains("percent")) {
      percent = parseFloat(number.value);
    }

    if ((value || value == 0) && (percent || percent == 0)) {
      resultText.innerHTML = calculate(value, percent);
    }
  });
});

function calculate(val, percent) {
  return (val * percent) / 100;
}
<div >
  <div >
    <span>Amount</span>
    <input type="number" >$
  </div>
  <div >
    <span>Percent</span>
    <input type="number" >% = $
    <span ></span>
  </div>
</div>

Please I need help

CodePudding user response:

If I understood correctly, you want to add the result to the typed Amount.

Add the value to your calculate function :

function calculate(val, percent) {
    return (val * percent) / 100   value;
}
  • Related