...

View Full Version : Frustrated !!!



Grandpa "D"
09-27-2012, 02:52 AM
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.


<!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);
};

felgall
09-27-2012, 03:03 AM
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.

Old Pedant
09-27-2012, 03:09 AM
Doesn't make sense.



// 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:


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.

Old Pedant
09-27-2012, 03:13 AM
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".

Grandpa "D"
09-27-2012, 03:22 AM
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.

Old Pedant
09-27-2012, 03:28 AM
Heh...my comment about the labels on the buttons was actually a mildly snide remark about Felgall's post. It was naughty of me.

felgall
09-27-2012, 03:29 AM
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.

Old Pedant
09-27-2012, 03:29 AM
I am an idiot. I got the numbers backwards. You had them in the right places; just had the variable names wrong.



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);
};

Old Pedant
09-27-2012, 03:30 AM
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.

Grandpa "D"
09-27-2012, 03:44 AM
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.

Old Pedant
09-27-2012, 08:03 PM
??? Worked perfectly correctly for me.

I put the JS inline rather than in a file, but that would make no difference.


<!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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum