Okay. Even though I'm about to start a job, I'm going to college, and completing assignments. One assignment is constructing a temperature converter in JavaScript, between Celsius, Fahrenheit, Kelvin and Newton. This is just my test version - the full thing checks 10 different scales. The second part requires you display a message relating to specified temperatures if the answer returned matches one of a list.
Anyway, to convert from one to another, I all up a formula, which I'm certain is saved as a string.
This will explain it better, here's the function that does the work:
t1 is a number entered by the user as the value of a temperatire in, let's call it, scale1, and the designate what scale they want to convert to. Another function returns the formula. Examples:
So in Celsius to Kelvin, the formula is ' -273.15'
It started off as this:
I have since gone through several different versions, below is the latest
I've tried .toFixed(1), but it just says it's "not a function".
Bottom line, I just need a way for the constructed formula string to run as aa mathematical formula and give me a number. I'm just hitting my head against a wall here!
Any help appreciated.
ETA:
Okay, I seem to have fixed it.
What was being passed in was a mix of numbers and strings. So I simply set the vars as eg, var tnum = ''; instead of var tnum = 0;
That seems to have cracked it. Yes I used eval, but in this case it seems to be a necessary eval
Still have a small problem
I have a list of msgs to put in for specific returned temperatures, eg, kelvin 0.0 = 'Absolute Zero', Fahrenheit 0.0 = 'Fahrenheit's ice/salt mixture', Celsius 100 = 'Boiling point of water', and so on.
The problem is simple. If the calculation returns a number that is 0.1 different to the stored figure, then no msg, I'm thinking about a calculation that says if it's +/- 0.5, to display the msg. I can work out how to do that, but any suggestions as to how else could be done?
The temperature list is under 'Comparison of temperature scales' in:
http://en.wikipedia.org/wiki/Temperature_conversion
Anyway, to convert from one to another, I all up a formula, which I'm certain is saved as a string.
This will explain it better, here's the function that does the work:
t1 is a number entered by the user as the value of a temperatire in, let's call it, scale1, and the designate what scale they want to convert to. Another function returns the formula. Examples:
(Yes, I've worked out a way to pass in the opening bracket in the 2nd example, it's not used in this function).Kelvin to Celsius [°C] = [°K] - 273.15
Fahrenheit to Kelvin [°K] = ([°F] + 459.67) × 5/9
So in Celsius to Kelvin, the formula is ' -273.15'
It started off as this:
I NEED res to be the answer to the equation as a number. What this seems to do is instead copy the formula ('23 - 273.15') as a string, reassigning res as a string as well.function convert1(t1, formula){
var res = 0;
var tnum = 0;
tnum = t1.concat(' ' , formula);
res = tnum;
return res;
}
I have since gone through several different versions, below is the latest
This one returns NaN, because the string is being passed to tnum.function convert1(t1, formula){
var res = 0;
var formula_construct = '';
var tnum = 0;
formula_construct = t1.concat(' ' , formula);
tnum = formula_construct;
tnum = Math.round(tnum*10)/10;
res = tnum;
return res;
}
I've tried .toFixed(1), but it just says it's "not a function".
Bottom line, I just need a way for the constructed formula string to run as aa mathematical formula and give me a number. I'm just hitting my head against a wall here!
Any help appreciated.
ETA:
Okay, I seem to have fixed it.
What was being passed in was a mix of numbers and strings. So I simply set the vars as eg, var tnum = ''; instead of var tnum = 0;
function convert1(t1, formula){
var res = 0;
var tnum = '';
tnum = t1.concat(' ' , formula);
res = eval(tnum);
return res.toFixed(1);
}
That seems to have cracked it. Yes I used eval, but in this case it seems to be a necessary eval

Still have a small problem
I have a list of msgs to put in for specific returned temperatures, eg, kelvin 0.0 = 'Absolute Zero', Fahrenheit 0.0 = 'Fahrenheit's ice/salt mixture', Celsius 100 = 'Boiling point of water', and so on.
The problem is simple. If the calculation returns a number that is 0.1 different to the stored figure, then no msg, I'm thinking about a calculation that says if it's +/- 0.5, to display the msg. I can work out how to do that, but any suggestions as to how else could be done?
The temperature list is under 'Comparison of temperature scales' in:
http://en.wikipedia.org/wiki/Temperature_conversion
Last edited: