简体   繁体   English

将值从一个函数移到另一个函数

[英]Moving a Value From One Function to Another

I am new to javascript and I am trying to learn the programming language. 我是javascript新手,正在尝试学习编程语言。 I created a quick website that converts temperature from fahrenheit to Celsius and I am using javascript to do the conversion. 我创建了一个快速网站,将温度从华氏温度转换为摄氏温度,并且我正在使用JavaScript进行转换。 I know that it does not have to be broken into two different functions but I though I would start simple and build towards more complex. 我知道不必将它分为两​​个不同的功能,但是尽管我会从简单开始,朝着更复杂的方向发展。 The code I am using to complete the conversion is: 我用来完成转换的代码是:

 function toCelcius(){ var temp = document.getElementById("tempConversion").value; var dif = calculate(temp); var c = (5/9) * dif; document.getElementById("answer").innerHTML = c; } function calculate(temp){ var dif = temp - 32; document.getElementById("firstCalc").innerHTML = dif; } 

I know that NaN means not a number so I am assuming that the line, var dif = calculation(temp), is not defined properly. 我知道NaN的意思不是数字,因此我假设var dif = Calculation(temp)的行定义不正确。 Unfortunately, I do not understand why and could use your help understanding. 不幸的是,我不明白为什么,可以使用您的帮助理解。

Thank you very much. 非常感谢你。

function calculate(temp){
    var dif = temp - 32;
    document.getElementById("firstCalc").innerHTML = dif;
    return dif; // return the value here...
}

function toCelcius(){
    var temp = document.getElementById("tempConversion").value;
    var dif = calculate(temp); // returned value will be assigned in "dif"
    var c = (5/9) * dif;
    document.getElementById("answer").innerHTML = c;
}

Your function calculate(temp) is not returning a value which is being needed. 您的函数calculate(temp)没有返回需要的值。 As a result, dif will be undefined and it will give you NaN when you perform any arithmetic operations there. 结果, dif将是未定义的,并且在该处执行任何算术运算时将为您提供NaN

Update you function like this - 这样更新您的功能-

function calculate(temp){

    var dif = temp - 32;

    document.getElementById("firstCalc").innerHTML = dif;
    return dif;

}

When getting a value back from a function, instead of setting a variable, try using return like I did. 从函数取回值时,而不是设置变量,请像我一样尝试使用return Note that in calculate() , the variable doesn't have to be named dif . 请注意,在calculate() ,变量不必命名为dif Really try to understand what's happening here, as a more complete understanding will help you in the long run. 真正尝试理解这里发生的事情,因为从长远来看,更全面的了解将对您有所帮助。

 function toCelcius(){ var temp = document.getElementById("tempConversion").value; var dif = calculate(temp); var c = (5/9) * dif; document.getElementById("answer").innerHTML = c; } function calculate(temp){ var dif = temp - 32; document.getElementById("firstCalc").innerHTML = dif; return dif; } 
 <input type="text" id="tempConversion"></input> <br/> <button type="button" onClick="toCelcius();">Convert</button> <br/> <span id="firstCalc"></span> <br/> <span id="answer"></span> 

Essentially, since you didn't use return , no matter whet you set dif to (not to mention that using var separated the two dif variables, so the first one wouldn't get set), it would immediately get set to what you returned from calculate() -- which was undefined . 本质上,由于您没有使用return ,所以无论您将dif设置为什么(更不用说使用var分隔两个dif变量,因此第一个都不会设置),它将立即设置为您返回的值来自calculate() - undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM