将数字输入传递给函数。使用Javascript

时间:2018-02-05 13:21:43

标签: javascript

我正在尝试将数字输入传递给函数并获得答案。这是我到目前为止所拥有的。

var x = firstProblem();

function firstProblem(firstNumber, secondNumber) {
  return firstNumber * secondNumber;
}

document.getElementById("first").innerHTML = x;
#firstNumber {
  height: 3em;
  font-size: 2em;
}

#secondNumber {
  height: 3em;
  font-size: 2em;
}
<p>If I save <input type="number" id="firstNumber" value="0"> a week how many weeks would It take to get to <input type="number" id="secondNumber" value="0"></p>




<p id="first"></p>

1 个答案:

答案 0 :(得分:0)

const first = document.getElementById('first');
const firstNumber = document.getElementById('firstNumber');
const secondNumber = document.getElementById('secondNumber');

firstNumber.addEventListener('change', onChange);
secondNumber.addEventListener('change', onChange);

function onChange() {
  first.innerHTML = firstProblem(firstNumber.value, secondNumber.value);
}

function firstProblem(firstNumber, secondNumber) {
  return firstNumber * secondNumber;
}