创建计算器功能

时间:2019-12-19 01:05:36

标签: javascript jquery html

我正在使用加法和乘法来编码一个简单的计算器功能。我可以进行加法运算,但是当我添加乘法函数时,没有显示任何值。我是一个新手,以为我理解了,但是我错过了一些东西。

我的代码是:

$(document).ready(function() {
  $("#action").click(function() {
    //DO NOT CHANGE CODE BELOW
    var num1 = parseInt($("#num1").val());
    var num2 = parseInt($("#num2").val());
    clear();
    addNumbers(num1, num2);
    var result = multiplyNumbers(num1, num2);
    $("#result-mult").text(result);
    //DO NOT CHANGE CODE ABOVE
  });

  /*
     Below this comment, create a function 
     named addNumbers, which accepts two parameters.
     The function should add the two parameters together
     and write the result to the element with the id
     result-add
    */
});

function addNumbers(num1, num2) {
  var retVal = num1 + num2;
  $("#result-add").html(retVal);
}

/*
	 Below this comment, create a function
	 named multiplyNumbers, which accepts two parameters.
	 The function should multiply the two parameters together
	 and return the result.
	*/
function multiplyNumbers(num1, num2) {
  var myResult = num1 * num2;
  $("#result-mult").html(myResult);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Assignment 3 </title>
  <meta charset="utf-8">
  <script src="js/jquery-3.4.1.js"></script>
  <script src="js/assignment03.js"></script>

</head>

<body>

  <h1>Calculator</h1>
  <label for="num1">Number1</label>
  <br>
  <input id="num1" name="num1" type="text" value="0" />
  <br>
  <label for="num2">Number 2</label>
  <br>
  <input id="num2" name="num2" type="text" value="0" />
  <br>
  <br>
  <a id="action" href="#">Click Here to Calculate </a>

  <div>The result of adding the numbers is: <span id="result-add"></span></div>
  <div>The result of multiplying the numbers is: <span id="result-mult"></span></div>
</body>

</html>

0 个答案:

没有答案