首页 > 解决方案 > 创建计算器函数

问题描述

我正在使用加法和乘法编写一个简单的计算器函数。我得到了加法,但是当我添加乘法函数时,没有任何值出现。我是这方面的新手,并认为我理解它,但我错过了一些东西。

我的代码是:

$(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>

标签: javascriptjqueryhtml

解决方案


推荐阅读