使用闭包的数学运算 [英] Mathematical operations using closures

查看:85
本文介绍了使用闭包的数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用闭包功能的计算器操作员。



未按用户选择显示结果请给我帮助

<结果块中的
在将其转换为整数后也显示为NaN。



我尝试过:



// HTML代码

It is a calculator operator using a closure function.

It is not showing result by user selection please give me help

in the result block it is showing as a NaN after converting it to an Integer also.

What I have tried:

//HTML code

<pre><html>
	<head>
		<title> Closures on Arithmatic Operations</title>
		<script type="text/javascript" src="jquery.js"></script>
		<script type="text/javascript" src="ClosuresArithmatic.js"></script>
		<script type="text/javascript" src="mathematicalOperatons.js"></script>
				<style type="text/css">
			.block{
				width: 500px;
				height: 500px;
				border: 1px solid black;
				background-color: #ccc;
			}
			li{
				list-style: none;
				padding: 5px;
			}
			#showvalues{
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="block">
			<ul>
				<li>
					Enter the First Number : <input type="text" name="fnumber" id="fnumber">
				</li>
				<li>
					Enter the second Number : <input type="text" name="fnumber" id="snumber">
				</li>
				<li> Type of Arithmatic Operations
					<select id="operation">
						<option value="">Arithatic Operations</option>
						<option value="add">Addition</option>
						<option value="sub">Subtraction</option>
						<option value="mul">Multiplication</option>
						<option value="divn">Division</option>
					</select>
				</li>
			</ul>
			<input type="button" value="Submit Values"  onclick="return calculate_operations()">
		<div id="showvalues">
			<ul>
				<li>
					Entered first Number is : <span id="numberF" />
				</li>

				<li>
					Entered second Number is : <span id="numberS" />
				</li>
				<li>
					Type of Operation Selected : <span id="sel_operation"></span>
				</li>
				<li>
					Result is : <span id="result_block" /> 
				</li>
			</ul>
			</div>
		</div>
	</body>
</html>







// Java脚本代码




// Java Script Code

function calculate_operations(){
	var details = {}
		details.Firstvalue = document.querySelector("#fnumber").value;
		details.SecondValue = document.querySelector("#snumber").value;
		details.Firstvalue = parseInt(details.Firstvalue);
		details.SecondValue = parseInt(details.SecondValue);
		details.result = 0;
		//details.selectedOperation = document.querySelector("operation")
		details.showdetails = function(){
			//console.log(pradeep);
			document.querySelector("#numberF").innerText = details.Firstvalue;
			document.querySelector("#numberS").innerText = details.SecondValue;
			function operationsMathematical(){
				if (document.querySelector("#operation".value == "")) {
				document.querySelector("#sel_operation").innerText = +"No operation selected";
				}
				if (document.querySelector("#operation").value== "add") {
					document.querySelector("#sel_operation").innerHTML =+details.Firstvalue +"+" +details.SecondValue ;
					details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue);
					details.result=parseInt(pradeep.manupulation_value(sum_is));
					document.querySelector("#result_block").innerHTML = +details.result;

				}
				if (document.querySelector("#operation").value== "sub") {
					document.querySelector("#sel_operation").innerText =+details.Firstvalue  +"-" +details.SecondValue;
					details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue);
					details.result=parseInt(pradeep.manupulation_value(sub_is));
					document.querySelector("#result_block").innerHTML = +details.result;
				}
				if (document.querySelector("#operation").value== "mul") {
					document.querySelector("#sel_operation").innerText =+details.Firstvalue +"*" +details.SecondValue;
					details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue);
					details.result=parseInt(pradeep.manupulation_value(mul_is));
					document.querySelector("#result_block").innerHTML = +details.result;
				}
				if (document.querySelector("#operation").value== "divn") {
					document.querySelector("#sel_operation").innerText =+details.Firstvalue +"/" +details.SecondValue;
					details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue);
					details.result=parseInt(pradeep.manupulation_value(divn_is));
					document.querySelector("#result_block").innerHTML = +details.result;
				}
			}
			operationsMathematical();
		}
	//details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue);
	document.querySelector("#showvalues").style.display="block";
	details.showdetails();
}



//关闭代码


//Closure Code

var pradeep=(function(){
	//result = 0; sum_is=sub_is=mul_is=divn_is=0; 
	function manupulation(x,y){
		var sum_is = getsumValues(x,y);
		var sub_is = getsubValues(x,y);
		var mul_is = getmulValues(x,y);
		var divn_is = getdivnValues(x,y);
		return [sum_is,sub_is,mul_is,divn_is];
	}
	function getsumValues(x,y){
		var sum_is = x+y;
		return sum_is; 
	}
	function getsubValues(x,y){
		var sub_is = x-y;
		return sub_is;
	}
	function getmulValues(x,y){
		var mul_is = x*y;
		return mul_is;
	}
	function getdivnValues(x,y){
		var divn_is = x/y;
		return divn_is;
	}return{
		manupulation_value : function(x,y){
			return manupulation(x,y);
		}
	}
})();

推荐答案

Quote:

details.result = pradeep.manupulation_value(details.Firstvalue, details.SecondValue);
details.result = parseInt(pradeep.manupulation_value(sum_is));



调试代码并逐步执行以下几行:


Debug your code and step through these lines:

  1. pradeep.manupulation_value 返回一个包含四个值的数组,表示结果所有四个操作;
  2. operationsMathematical 函数中, sum_is 不是已定义的变量;
  3. 然后尝试再次调用 pradeep.manupulation_value ,传递一个未定义的变量,并跳过第二个参数;
  4. 根据浏览器的不同,第二次调用的结果将是一个包含四个 NaN 值的数组,或一个运行时错误,告诉你 sum_is 未定义;
  5. 假设调用成功并返回一个数组, parseInt 会将数组转换为一个字符串(NaN,NaN,NaN,NaN),然后尝试将该字符串解析为整数;
  6. 不出所料,结果将是 NaN
  1. pradeep.manupulation_value returns an array of four values representing the results of all four operations;
  2. Within the operationsMathematical function, sum_is is not a defined variable;
  3. You then try to call pradeep.manupulation_value again, passing one undefined variable, and skipping the second argument;
  4. Depending on the browser, the result of this second call will either be an array of four NaN values, or a run-time error teliing you that sum_is is not defined;
  5. Assuming the call succeeds and returns an array, parseInt will convert the array to a string ("NaN,NaN,NaN,NaN"), and then try to parse that string as an integer;
  6. Unsurprisingly, the result will be NaN.



替换每个中的那些行if 以下块:


Replace those lines in each of your if blocks with:

var result = pradeep.manipulation_value(details.Firstvalue, details.SecondValue);
details.result = result[0];

替换 0 返回数组中的相应索引。

replacing 0 with the appropriate index within the returned array.


你的代码有bug,工具是调试器。



当你不知道了解您的代码正在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



调试器在这里向您展示您的代码正在做什么以及您的代码任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Your code have bugs, the tool is the debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于使用闭包的数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆