简体   繁体   English

如何在此脚本代码中使此计算和变量正确?

[英]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.

相关问题 如何为变量简化代码? - How to make Code Simplified for Variables? 您如何清除用于执行下一个正确计算的变量? - How do you clear variables used in order to perform the next correct calculation? 当Javascript变量为“正确”时,如何使某些内容出现在HTML网页上? - How to make something appear on an HTML webpage when Javascript variables are 'correct'? 如何使用 JavaScript Array.find() 方法对对象数组进行正确的算术计算? - How to use JavaScript Array.find() method to make correct arithmetical calculation in array of objects? 如何在函数内部计算变量 - how to calculation variables inside a function 使用变量在js中进行计算时无法获得正确的结果 - Cannot get correct result when using variables to do calculation in js 如何使JavaScript脚本中的所有变量都有两个小数点 - How to make all variables in javascript script have two decimal points 如何将这一系列的java脚本变量变成一个函数? - How can I make this series of java script variables into a function? 如何使文本在具有 2 个变量的按钮脚本上水平滚动? - How to make text scroll horizontally on a button script with 2 variables? 如何将一个function返回的值输入到另一个function中进行计算,而不需要运行两次代码? - How to input the value returned in one function to another function so as to make a calculation, without running the code twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM