Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Help with js

  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with js

    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.

    Code:
    <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>
    Last edited by codingcodeddode; 11-01-2012 at 11:00 PM. Reason: fixed code BB tags

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,607
    Thanks
    6
    Thanked 997 Times in 970 Posts
    Wow, you must be the first one that doesn’t want an answer.
    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.

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    VIPStephan read your code wrong. They exist.

    Hang on a sec...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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
    Code:
    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"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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
    Code:
    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

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Gotcha.

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

    Also, technically when you do this:
    Code:
    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:
    Code:
    var farenheit = Number( document.getElementById('f1').value );
    if(farenheit == 0){
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var farenheit = Number( document.getElementById('f1').value );
    You can also convert a string to a number using:

    Code:
    var farenheit = +document.getElementById('f1').value;
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Yes, but I saw he had used Number() in his code so I assumed he would understand using it.

    You can also use
    Code:
    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
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •