2017-02-25 133 views
0

它沒有顯示結果通過用戶選擇,請給我幫助使用閉包計算數學運算...?

它在結果框是顯示它轉換爲整數後也爲NaN的使用閉合功能

一個計算器運算。

Mathamatical.js //關閉內容

var pradeep=(function(){ 
    result = 0; sum_is=sub_is=mul_is=divn_is=0; 
    function manupulation(x,y){ 
     sum_is = getsumValues(x,y); 
     sub_is = getsubValues(x,y); 
     mul_is = getmulValues(x,y); 
     divn_is = getdivnValues(x,y); 
     return result[sum_is,sub_is,mul_is,divn_is]; 
    } 
    function getsumValues(x,y){ 
     var sum_is = x+y; 
     return result[0]; 
    } 
    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); 
     } 
    } 
})(); 

我的第二個js文件

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); 
       //console.log(details.Firstvalue,details.SecondValue); 
       details.result=parseInt(pradeep.manupulation_value(result[0])); 
       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(result[1]); 
       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(result[sum_is])); 
       document.querySelector("#result_block").innerHTML = +details.result; 
      } 
      if (document.querySelector("#operation").value== "divn") { 
       document.querySelector("#sel_operation").innerText =+details.Firstvalue +"/" +details.SecondValue; 
      } 
     } 
     operationsMathematical(); 
    } 
    //details.result= pradeep.manupulation_value(details.Firstvalue,details.SecondValue); 
    document.querySelector("#showvalues").style.display="block"; 
    details.showdetails(); 
} 
+2

你認爲'返回結果[sum_is,sub_is,mul_is,divn_is];'確實'result'是'0'嗎?它*實際*做的是返回'undefined';你可能的意思是返回[sum_is,sub_is,mul_is,divn_is];'返回一個包含結果的數組。你的'getSumValues()'函數有一個類似的問題。 (所以問題不在關閉......) – nnnnnn

+0

在'getsumValues'中,即使'result = 0'(它是一個數字,而不是一個數組),你正在返回'result [0]' –

+0

謝謝..即使雖然它顯示sum_is在返回數據時未定義的錯誤。 –

回答

0

改變的文件。這應該工作

var pradeep=(function(){ 
    // No need of these variables result = 0; sum_is=sub_is=mul_is=divn_is=0; 
    function manupulation(x,y){ 
     var sum_is = getsumValues(x,y); //use var otherwise global scope is used 
     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); 
     } 
    } 
})(); 

您的第二個文件有錯誤。

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); 
        //console.log(details.Firstvalue,details.SecondValue); 
        details.result=parseInt(pradeep.manupulation_value(result[0])); 
        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(result[1]); 
        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(result[2])); 
        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(result[3]); 
       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(); 
    } 
+0

即使它不工作。顯示sum_is未定義的錯誤 –