0

I am having trouble displaying a math function, there is nothing stated wrong in the console, so I do not know where I am going wrong. the output does not display the correct answer here...

Desired outcome: enter number in each input, and javascript multiplies those two input values then displays the result when you click the button.

var money = document.getElementById('money').value;
    var years = document.getElementById('years').value;
    var output = document.getElementById('output');
    var myOutput = money * years;
    const btn = document.getElementById('btn');


    btn.addEventListener('click', () => {
      output.innerHTML = myOutput;
    })
<body>
  <h4>how much money do you make a year?</h4>

  <input id="money" type="number" placeholder="$$$"></input>
  <input id="years" type="number" placeholder="years"></input>

  <div id="output">

  </div>

  <button id="btn" type="button">go</button>

</body>

3
  • yes i noticed this and was about to edit, however, when fixed, the code still does not display the correct answer.... Commented Sep 22, 2017 at 20:35
  • You've got some fundamental misunderstandings of how imperative programming languages work. Your declaration of myOutput does not establish a permanent relationship between that variable and other values; it's a request to perform that computation once, at the time the variable is declared. Commented Sep 22, 2017 at 20:36
  • yeh, I only want it displayed once, then reset on window refresh... Commented Sep 22, 2017 at 20:38

4 Answers 4

1

I would structure it like this:

const btn = document.getElementById('btn');
const money = document.getElementById('money');
const years = document.getElementById('years');
const output = document.getElementById('output');

btn.addEventListener('click', () => {
  output.innerHTML = Number(money.value) * Number(years.value);
})
<body>
  <h4>how much money do you make a year?</h4>

  <input id="money" type="number" placeholder="$$$"></input>
  <input id="years" type="number" placeholder="years"></input>

  <div id="output">

  </div>

  <button id="btn" type="button">go</button>

</body>

As others have said, you need to move the logic inside the click handler. (In your code as it's structured, you get the two values once, at the load of the script and never update them.)

I have also broken out the searching of the DOM nodes from the calculation; it's probably a good practice for anytime such changes can happen more than once.

Finally, I converted the String values you'll get from the form elements into numbers before doing any work with them. This is generally necessary, although because of some Javascript magic, you don't actually have to do it here. (Try changing from multiplication to addition to see the dangers of forgetting this.)

Sign up to request clarification or add additional context in comments.

Comments

1

When you init your application, your input fields don't have any value filled yet.

var money = document.getElementById('money');
var years = document.getElementById('years');
var output = document.getElementById('output');
const btn = document.getElementById('btn');

function calc(val1, val2) {
	return Number(val1.value) * Number(val2.value);
}


btn.addEventListener('click', (e) => {
  output.innerHTML = calc(money, years);
});
<body>
  <h4>how much money do you make a year?</h4>

  <input id="money" type="number" placeholder="$$$"></input>
  <input id="years" type="number" placeholder="years"></input>

  <div id="output">

  </div>

  <button id="btn" type="button">go</button>

</body>

5 Comments

ah alright, I was thinking of this, but I wasn't sure how to create a value for it besides "money = 0;" or something of the sort.
This answer and my own are very similar. The only essential difference is that there is no conversion of the String form values to numbers. That could cause problems with other forumulas.
Hi @hannacreed, I didn't understand your comment, can you clarify, please?. In addition, there's no need to convert the string values to numbers, JavaScript handles this for you, just keep the input types "number".
@PabloDarde Try changing from multiplication to addition and you'll see the issue.
I got, you are alright! This occurs because input.value return a string type. The fix is cast to Number. See the updated solution.
0

You need to set the innerHTML of the element. You want to get the user input values after the click of the button. Therefore, you move your variables inside the callback function

    const btn = document.getElementById('btn');


    btn.addEventListener('click', () => {
      var money = document.getElementById('money').value;
    var years = document.getElementById('years').value;
    var output = document.getElementById('output');
    output.innerHTML = money * years;
    })
<body>
  <h4>how much money do you make a year?</h4>

  <input id="money" type="number" placeholder="$$$"></input>
  <input id="years" type="number" placeholder="years"></input>

  <div id="output">

  </div>

  <button id="btn" type="button">go</button>

</body>

Comments

0

Try this set inner html of div and have everything but the btn element happen on click:

    const btn = document.getElementById('btn');

    btn.addEventListener('click', () => {
    var money = document.getElementById('money').value;
    var years = document.getElementById('years').value;
    var output = document.getElementById('output');
    var myOutput = money * years;

      output.innerHTML = myOutput;
    })
<body>
  <h4>how much money do you make a year?</h4>

  <input id="money" type="number" placeholder="$$$"></input>
  <input id="years" type="number" placeholder="years"></input>

  <div id="output">

  </div>

  <button id="btn" type="button">go</button>

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.