[英]How to make this calculation and variables correct in this script code?
I feel like I'm progressing a little, I still have difficulties figuring out what to do when I'm stuck with Javascript. 我觉得我正在进步一点,当我遇到Javascript时,我仍然很难搞清楚要做什么。 It's very hard but I need to get this coding done urgently.. so any help is greatly appreciated. 这很难,但我需要紧急完成这个编码..所以任何帮助都非常感谢。
It's really simple, I want to make my own converter from Kelvin, Celsius and Fahrenheit. 这很简单,我想用Kelvin,Celsius和Fahrenheit制作我自己的转换器。 So I made these 3 variables, but I kind of realised they need their own formula, so do I need a different variable for the result? 所以我做了这3个变量,但我意识到他们需要自己的公式,所以我需要一个不同的结果变量吗? And if so where does it go? 如果是这样,它会去哪里? All these functions are so confusing. 所有这些功能都令人困惑。 This is my code. 这是我的代码。
<form>
Kelvin is
<input id="kelvin" size="7" maxlength="5" type="text" placeholder="vul in" />
<p></p>
Celsius is
<input id="celsius" size="7" maxlength="9" type="text" placeholder="vul in" />
<p></p>
Fahrenheit is
<input id="fahrenheit" size="7" maxlength="9" type="text" placeholder="vul in" />
<p></p>
<input id="calculate" type="button" value="Bereken!" />
</form>
<div id="calc">Dit is kelvin
<p></p>dit is celsius
dit is fahrenheit dit是华氏度
and then the script 然后是脚本
<table cellSpacing=0 cellPadding=0 width=250 border=0>
document.getElementById('calculate').addEventListener('click', function() {
var kel= document.getElementById("kelvin").value;
var cel = document.getElementById("celsius").value;
var far = document.getElementById("fahrenheit").value;
var div = document.getElementById('calc');
if (( kel < 0 || cel < -273 || far < -459 || isNaN(kel) || isNaN(bev)) {
div.innerHTML = "Not valid!";
return;
}
kel = parseInt(kelvin); cel = parseInt(celsius); far = parseInt (fahrenheit);
var far = (cel * (9/5) + 35;
var kel = cel + 273;
var cel = kel - 273;
var cel = (far -32)*(5/9);
if (far = kel ) {
var text = "hello? what to do here";
}
div.innerHTML = "Het is <b>" + kelvin+ "</b> Kelvin <p></p> en het is <b>" + celcius + "</b>" en het is <b>" + fahrenheit + "</b>";
}, false);
First of all 首先
if (far = kel ) {
var text = "hello? what to do here";
}
should be 应该
if (far === kel ) {
var text = "hello? what to do here";
}
= is used to define variables eg. =用于定义变量,例如。 var a = 10; var a = 10;
=== is used to compare two values ===用于比较两个值
Also, you put 还有,你说
<table cellSpacing=0 cellPadding=0 width=250 border=0>
in the middle of the script. 在脚本中间。 Which I hope was a mistake. 我希望这是一个错误。
Which is best written 哪个写得最好
<table cellspacing='0' cellpadding='0' width='250' border='0'>
To be compliant with newer stricter XHTML standards. 符合更严格的XHTML标准。
Also, this: 这个:
if (( kel < 0 || cel < -273 || far < -459 || isNaN(kel) || isNaN(bev)) {
div.innerHTML = "Not valid!";
return;
}
needs to be replaced by this: 需要替换为:
if ( kel < 0 || cel < -273 || far < -459 || isNaN(kel) || isNaN(cel) || isNaN(far)) {
document.getElementById('calc').innerHTML = "Not valid!";
}
And this: 和这个:
kel = parseInt(kelvin); cel = parseInt(celsius); far = parseInt (fahrenheit);
Should read: 应该读:
kel = parseInt(document.getElementById("kelvin").value); cel = parseInt(document.getElementById("celcius").value); far = parseInt (document.getElementById("fahrenheit").value);
Claies has a good point too. 克莱斯也有一个好处。
if(kel != ''){
//Kelvin is the chosen one
}else if(far != ''){
//Fahrenheit is the chosen one
}else if(cel != ''){
//Celcius is the chosen one
}else{
//User hasn't written anything
alert('You need to write something to convert!');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.