...

View Full Version : Help with js



codingcodeddode
11-01-2012, 11:23 PM
For a JS class i need to get this to work, why isnt it working? DONT give me the answer just point me in the right direction.



<html>
<head>
<title>

</title>

<script>

var check = function(){
var celcius = document.getElementById('c1').value;
var farenheit = document.getElementById('f1').value;

if(farenheit == 0){

var c2f = function(){
var celcius = document.getElementById('c1').value;
document.getElementById('f1').value = (Number(1.8) * celcius) + 32;
}


}else if(celcius==0){



var f2c = function(){

var farenheit = document.getElementById('f1').value;
document.getElementById('c1').value = (farenheit-32)/1.8;

}

}
}
</script>

</head>

<body>

<form id='myForm' name='myForm' action="process.php" method="post">

<h1>Temp</h1>

<label>Celcuis</label>
<input id='c1' value='0'><br>

<label>Farenheit</label>
<input id='f1' value='0'><br>


<input type='button' onclick='check()' value='Convert'>



</form>
</body>

</html>

VIPStephan
11-02-2012, 12:10 AM
Wow, you must be the first one that doesn’t want an answer. :D
Well, I think the reason for it not working is because the elements don’t exist at the time the function is stored in the variable.

codingcodeddode
11-02-2012, 12:31 AM
I thought it didnt matter because the function isnt called until the button is clicked, and I dont click the button untill i enter a number so I would think the elements do exist by then. Im stumped :( just a beginner

Old Pedant
11-02-2012, 03:02 AM
VIPStephan read your code wrong. They exist.

Hang on a sec...

Old Pedant
11-02-2012, 03:14 AM
Okay, you have a fundamental misunderstanding here.

It's true the if fahrenheit is zero you create a function to convert celcius to fahrenheit.

But WHERE do you ever *CALL* that function?

That is, where in your code do you ever calls the function c2f????

Or, in the other case, where do you ever call f2c???

Answer: Nowhere.

There is NO REASON AT ALL for the existence of the functions c2f and f2c. You can simply get rid of them.

If you did nothing but wipe out var c2f = function() and var f2c = function() (leaving all the { and } alone) your code would run.

&&&&&&&&&&&&&&

Now, is it then efficient?

No.

Inside of each of those now-nonexistent functions, you do


var celcius = document.getElementById('c1').value;
and
var farenheit = document.getElementById('f1').value;

Why?

You already did those *EXACT* same lines as the first two lines of your check() function.

And, finally, what happens if a person really *WANT* to convert 0 degrees celcius to fahrenheit? Or vice versa?

Think seriously about rewriting this.

Get rid of the check() function.

Keep the f2c and c2f functions, ONLY, but invoke each from a separate button, one labeled "Convert to fahrenheit" and the other "Convert to celcius"

codingcodeddode
11-02-2012, 06:28 PM
Okay, you have a fundamental misunderstanding here.

It's true the if fahrenheit is zero you create a function to convert celcius to fahrenheit.

But WHERE do you ever *CALL* that function?

That is, where in your code do you ever calls the function c2f????

Or, in the other case, where do you ever call f2c???

Answer: Nowhere.

There is NO REASON AT ALL for the existence of the functions c2f and f2c. You can simply get rid of them.

If you did nothing but wipe out var c2f = function() and var f2c = function() (leaving all the { and } alone) your code would run.

&&&&&&&&&&&&&&

Now, is it then efficient?

No.

Inside of each of those now-nonexistent functions, you do


var celcius = document.getElementById('c1').value;
and
var farenheit = document.getElementById('f1').value;

Why?

You already did those *EXACT* same lines as the first two lines of your check() function.

And, finally, what happens if a person really *WANT* to convert 0 degrees celcius to fahrenheit? Or vice versa?

Think seriously about rewriting this.

Get rid of the check() function.

Keep the f2c and c2f functions, ONLY, but invoke each from a separate button, one labeled "Convert to fahrenheit" and the other "Convert to celcius"

The thing is i have to follow instructions, and the instructions tell me to use 1 button and to have a function check which box has data, and depending on that do the function

Old Pedant
11-02-2012, 09:03 PM
Gotcha.

You might want to allow for EITHER zero or blank meaning "get this value".

Also, technically when you do this:


var farenheit = document.getElementById('f1').value;
if(farenheit == 0){
farenheit will *NEVER* be == 0. It might be == "0".

Now, JavaScript will try to do the conversion for you, but it might do it wrong.

So better is:


var farenheit = Number( document.getElementById('f1').value );
if(farenheit == 0){

felgall
11-03-2012, 12:23 AM
var farenheit = Number( document.getElementById('f1').value );


You can also convert a string to a number using:



var farenheit = +document.getElementById('f1').value;

Old Pedant
11-03-2012, 01:37 AM
Yes, but I saw he had used Number() in his code so I assumed he would understand using it.

You can also use


var farenheit = 1 * document.getElementById('f1').value;
var farenheit = document.getElementById('f1').value / 1;
var farenheit = document.getElementById('f1').value - 0;
var farenheit = parseFloat(document.getElementById('f1').value );
and others



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum