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 11 of 11

Thread: Frustrated !!!

  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Frustrated !!!

    I can't get this to work and I have tried all kinds of things for days.
    I finally have to come here for help because I can't figure this out.
    Code:
    <!doctype html>
    <html>
    	<head>
    		<meta charset="UTF-8"/>
    		<title>JavaScript Miles to Kilometers Chapter-2 # 27</title>
    	</head>
    	<body>
    		<h1>Miles to Kilometers</h1>
    		<p>
    			<input type="text"   id="distance" />
    			<input type="button" id="m_to_k" value="Miles to Kilometers" />
    			<input type="button" id="k_to_m" value="Kilometers to Miles" />
    		</p>
    		<p id="result"></p>
    		<script src="m2k.js"></script>
    	</body>
    </html>
    
    
    //javascript page called: m2k.js
    
    	var report = function (miles, kilometers) {
    	document.getElementById("result").innerHTML = 
    		miles + kilometers;
    	};
    	
    	document.getElementById("k_to_m").onclick = function () {
    		var miles = document.getElementById("distance").value;
    		report(kilometers * 0.621371);
    	};
    	
    		document.getElementById("m_to_k").onclick = function () {
    		var kilometers = document.getElementById("distance").value;
    		report(miles * 1.60934);
    	};

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    In each case the content of the JavaScript functions makes no sense.

    The report() function is expecting two parameters to be passed to it but you only pass one. Also you are concatenating the two parameters together in what you are outputting.

    In each of the other two functions you set a variable from the value entered and then never reference it again. You then pass one parameter to report which is undefined times a number which will therefore always be undefined.

    Without knowing what the functions are supposed to be doing it is rather difficult to suggest exactly how to fix it though.
    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.

  • Users who have thanked felgall for this post:

    Grandpa "D" (09-27-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Doesn't make sense.

    Code:
    // here, your report function takes *TWO* arguments:  miles and kilometers
    	var report = function (miles, kilometers) {
    	    document.getElementById("result").innerHTML = 
    		miles + kilometers;
    	};
    	
    	document.getElementById("k_to_m").onclick = function () {
    		var miles = document.getElementById("distance").value;
    // but here, where you call it, you are using only one argument.
    // on top of that, the one argument doesn't make since, because you are multiplying
    // kilometers by 0.62 and yet (a) you don't even have a variable named kilometers
    // and (b) you don't multiply kilometers by 0.62 to get miles
    		report(kilometers * 0.621371);
    	};
    	
    		document.getElementById("m_to_k").onclick = function () {
    		var kilometers = document.getElementById("distance").value;
    // and here you have the same problem.  miles isn't defined and the formula is wrong.
    		report(miles * 1.60934);
    	};
    Try this:
    Code:
    	function report(miles, kilometers) {
    	    document.getElementById("result").innerHTML = 
    		miles + " miles is equal to " + kilometers + " kilometers";
    	};
    	
    	document.getElementById("k_to_m").onclick = function () 
            {
    		var kDist = document.getElementById("distance").value;
    		report(kDist * 1.60934, kDist);
    	};
    	
    	document.getElementById("m_to_k").onclick = function () 
            {
    		var mDist = document.getElementById("distance").value;
    		report(mDist, mDist * 0.621371);
    	};
    I purposely used the variables kDist and mDist to emphasize that they are not the same as the variables kilometers and miles in the report( ) function.
    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.

  • Users who have thanked Old Pedant for this post:

    Grandpa "D" (09-27-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Without knowing what the functions are supposed to be doing ...
    Ummm...I suppose I could be wrong, but I'm pretty sure the functions are supposed to convert kilometers to miles and vice versa. Given that the two buttons have values of value="Miles to Kilometers" and value="Kilometers to Miles".
    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.

  • Users who have thanked Old Pedant for this post:

    Grandpa "D" (09-27-2012)

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...I suppose I could be wrong, but I'm pretty sure the functions are supposed to convert kilometers to miles and vice versa. Given that the two buttons have values of value="Miles to Kilometers" and value="Kilometers to Miles".
    Yes, you are correct in your assumption that there are two buttons to chose from as seen in the HTML doc.
    Either one way or the other. The user inserts a numeric value in the text box and the formula is to come up with the value and display it.
    I will try your formula in a few seconds then study what you did to try to understand it better.
    Thank you for your time and expertise.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Heh...my comment about the labels on the buttons was actually a mildly snide remark about Felgall's post. It was naughty of me.
    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.

  • Users who have thanked Old Pedant for this post:

    Grandpa "D" (09-27-2012)

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...I suppose I could be wrong, but I'm pretty sure the functions are supposed to convert kilometers to miles and vice versa. Given that the two buttons have values of value="Miles to Kilometers" and value="Kilometers to Miles".
    Yes but both functions also call report passing one undefined parameter and it isn't clear what report is supposed to be doing so just what the functions are supposed to be passing to report() is unclear.
    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.

  • Users who have thanked felgall for this post:

    Grandpa "D" (09-27-2012)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    I am an idiot. I got the numbers backwards. You had them in the right places; just had the variable names wrong.

    Code:
    	function report(miles, kilometers) {
    	    document.getElementById("result").innerHTML = 
    		miles + " miles is equal to " + kilometers + " kilometers";
    	};
    	
    	document.getElementById("k_to_m").onclick = function () 
            {
    		var kDist = document.getElementById("distance").value;
    		report(kDist *  0.621371, kDist);
    	};
    	
    	document.getElementById("m_to_k").onclick = function () 
            {
    		var mDist = document.getElementById("distance").value;
    		report(mDist, mDist *  1.60934);
    	};
    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.

  • Users who have thanked Old Pedant for this post:

    Grandpa "D" (09-27-2012)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Teach me to make a snide remark. You are of course correct, Stephen. I just created my own version of report( ) and it might not be at all what he wanted.
    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.

  • Users who have thanked Old Pedant for this post:

    Grandpa "D" (09-27-2012)

  • #10
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Master,
    Why did you do this?
    report(kDist * 1.60934, kDist)

    Grasshopper learning from Master!
    BTW
    It now works right BUT where you added the script only works correct in one direction and doesn't make sense in the other direction.
    Last edited by Grandpa "D"; 09-27-2012 at 03:57 AM.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,885
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    ??? Worked perfectly correctly for me.

    I put the JS inline rather than in a file, but that would make no difference.
    Code:
    <!doctype html>
    <html>
    	<head>
    		<meta charset="UTF-8"/>
    		<title>JavaScript Miles to Kilometers Chapter-2 # 27</title>
    	</head>
    	<body>
    		<h1>Miles to Kilometers</h1>
    		<p>
    			<input type="text"   id="distance" />
    			<input type="button" id="m_to_k" value="Miles to Kilometers" />
    			<input type="button" id="k_to_m" value="Kilometers to Miles" />
    		</p>
    		<p id="result"></p>
    <script type="text/javascript">
    	function report(miles, kilometers) {
    	    document.getElementById("result").innerHTML = 
    		miles + " miles is equal to " + kilometers + " kilometers";
    	};
    	
    	document.getElementById("k_to_m").onclick = function () 
            {
    		var kDist = document.getElementById("distance").value;
    		report(kDist *  0.621371, kDist);
    	};
    	
    	document.getElementById("m_to_k").onclick = function () 
            {
    		var mDist = document.getElementById("distance").value;
    		report(mDist, mDist *  1.60934);
    	};
    </script>
    </body>
    </html>
    If I enter 3 and then click on "Miles to kilometers" I get this result:
    3 miles is equal to 4.82802 kilometers

    If I enter 10 and then click on "Kilometers to miles" I get this:
    6.21371 miles is equal to 10 kilometers

    How does that not make sense?
    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
    •