0

I feel like I'm progressing a little, I still have difficulties figuring out what to do when I'm stuck with 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. 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? 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

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); 
Cœur
  • 37,241
  • 25
  • 195
  • 267
azaela
  • 57
  • 1
  • 2
  • 9
  • So what is the issue ? – Mairaj Ahmad Mar 07 '15 at 04:15
  • well you seem to be trying to calculate using values from all 3 input boxes, but it seems like the user is only expected to put a value in one of them at a time? Your first step should be isolating which textbox has the value you want to convert. – Claies Mar 07 '15 at 04:16
  • @Claies I'd like to type in an amount in one of these text boxes, fahrenheit for example, and when you hit calculate, it'll show the celsius and kelvin – azaela Mar 07 '15 at 04:18
  • right, that would make sense, but that's not what you are doing. – Claies Mar 07 '15 at 04:19
  • @Claies that's quite obvious, I wouldn't ask a question here if I understood what I was doing – azaela Mar 07 '15 at 04:20
  • your code is so far from functioning correctly that it's not really answerable without writing the entire script for you. so, my suggestion is that you figure out how to isolate which textbox the user put a value in, and then work from there. – Claies Mar 07 '15 at 04:21
  • @Claies could you tell me how? that's what I'm using this site for haha... – azaela Mar 07 '15 at 04:23

2 Answers2

0

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;

=== 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.

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);
Oliver
  • 1,576
  • 1
  • 17
  • 31
  • Thanks but it helped me as far as telling me where some of the functions go, but I'm not sure what I should do about the calculations. – azaela Mar 07 '15 at 04:38
  • @azaela The calculations should probably be split into three functions within my other answer near the top. The actual conversions I can't help you with. – Oliver Mar 07 '15 at 05:23
0

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!');
}
Oliver
  • 1,576
  • 1
  • 17
  • 31