javascript - 创建计算器函数
问题描述
我正在使用加法和乘法编写一个简单的计算器函数。我得到了加法,但是当我添加乘法函数时,没有任何值出现。我是这方面的新手,并认为我理解它,但我错过了一些东西。
我的代码是:
$(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>
解决方案
推荐阅读
- excel - 如何确定我的代码是否成功创建了 oExec 和 nchromePro 对象?
- php - jQuery 没有在 Wordpress 上定义-smoothstate Js
- mysql - 插入数据并用排名填充列
- amazon-web-services - 在 AWS 中,我需要帮助以通过公共 ip 或公共 dns 拒绝 url 并仅允许通过域 (CNAME) 访问
- javascript - 我可以使用 MJML 像我的图片一样访问我的模板电子邮件吗?
- firebase - 使用 React-Native Expo 在云上进行对象检测
- python - 随机游走 Python 练习循环
- node.js - 在 AWS Lambda 上使用 mysql2 和 Express 时的 ETIMEDOUT
- python - KeyError: 'rows' 在尝试获取 couchdb 中的视图内容时
- python - 使用 Firefox 浏览器切换到框架后,Python Selenium 无法定位元素