• Welcome! The TrekBBS is the number one place to chat about Star Trek with like-minded fans.
    If you are not already a member then please register an account and join in the discussion!

Wretched JavaScript! Help Reqd!

Australis

Writer - Australis
Admiral
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:
Kelvin to Celsius [°C] = [°K] - 273.15
Fahrenheit to Kelvin [°K] = ([°F] + 459.67) × 5/9
(Yes, I've worked out a way to pass in the opening bracket in the 2nd example, it's not used in this function).
So in Celsius to Kelvin, the formula is ' -273.15'

It started off as this:

function convert1(t1, formula){
var res = 0;
var tnum = 0;
tnum = t1.concat(' ' , formula);
res = tnum;
return res;
}
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.

I have since gone through several different versions, below is the latest

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;
}
This one returns NaN, because the string is being passed to tnum.

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:
No that didn't crack it. Actually, it's weird. This WAS working, and now it isn't!

function convert1(t1, formula){
var res = 0;
var tnum = '';
tnum = t1.concat(' ' , formula);
tnum = '"' + tnum + '"';
res = eval(tnum);
return res.toFixed(1);
}
Now I get 2 error msgs,

1 saying "missing ; before statement" after the eval(). This seems to have been fixed by putting the quotes around it as seen above.

Now it's, "res.toFixed is not a function" Yes, I KNOW it's not a function, I want you to return a number to one decimal place! Arseholes! :klingon:

AND!!!

When it gets 'res', it's gets it as a formula! It doesn't turn it into a single number!:
tempconv1.jpg



What I want (and assumed eval would cover) is:
a: turn the formula string into a mathematical formula; and
b: end up with a single number I can pass into other parts of the code.

Come on, guys! Help a code monkey out!
 
Last edited:
Wow, no replies!

Okay, it's fixed. This worked:

function convert1(t1, formula){
var res;
var tnum;
var spg;
tnum = t1.concat(' ' , formula);
tnum = String(tnum);
spg = eval(tnum);
res = spg;
return res;
}

And that String() did the trick. It seems bloody obvious, but I've searched the net high and low for an answer to that, it was damned hard! And again, a necessary eval.

I've also worked out how to do the message thing using a combination of switches and if stmts. So it's all good.

What was funny was when I was searching, this page showed up on the first Google page. :)

Thanks for your help! :D
 
Glad your problem was solved. I would have helped, but that's all a foreign language to me ;)
 
Yeah, it's a funny language. I understand the need to do stuff client-side, but server-side PHP is pretty damn good, and if the functionality of that language could be implemented in the client, it'd be cool.

That said, I've grown to appreciate JS more. But every now and then it's just throws a complete spanner.
 
PHP > Java .... Ya I know this doesn't help but you solved the problem anyways :]
 
Yeah, it's a funny language. I understand the need to do stuff client-side, but server-side PHP is pretty damn good, and if the functionality of that language could be implemented in the client, it'd be cool.

Oh, but it can! :)

Make an AJAX call with all your variables to a remote PHP file on the web server that has all the formulas in it. Let PHP do all the heavy lifting and spit the result back into a target DIV layer in the client. The page doesn't reload and it behaves as if it was all done on the client side. Love me my AJAX!
 
And if you use something like jQuery the AJAX part of it would be really easy--you just have to define what data is sent to the server and what to do with what you get back (which in your case would just be the result of the calculation.)
 
Ahhhhh, but here's the rub. It's a JavaScript assignment. Not AJAX or PHP or JQuery. Agreed any of those could handle it (not that I'm terribly familiar with all this fandangled modern technology anyway. :D ) I'm supposed to solve it within JS And, as I said, I found a way, but it isn't 100% reliable, I think I've got it to 96%. But thanks for the suggestions, I will ahve to learn them eventually.
 
^^^ Understood. Although technically JQuery is pure JavaScript, it's just a framework and series of libraries that allow you to do some extra-cool things a little easier than in plan-vanilla JS.

On a side note, I'm very glad to see that the schools are still teaching pure JavaScript. Too many times I've seen these JQuery ninjas do some amazing things but they don't even know how to print out variables for debugging using a simple "alert" box command. What you're doing is very good and will be extremely useful for you if you wind up pursuing a career in web development.
 
Yeah, people should definitely know how to code JS without the benefit of any framework. Frameworks should be a supplement to your existing skills, not something you use instead of learning how JS works!
 
While JS is not my favourite thing (as you may have gathered :) ), I agree with both of you. It really is very useful to hand--code, same with PHP.

Having said that, I've seen the plans for HTMLv5. That'll be a game changer!
 
Yeah, although HTML5 is going to take many years to see wide adoption (with the exception of a few key features which are coming out already.)
 
If you are not already a member then please register an account and join in the discussion!

Sign up / Register


Back
Top