...

View Full Version : Creating a program without functions



freewavycrocket
03-18-2012, 08:59 PM
Hi,

I have to create a simple program in javascript (temperature converter) but without the use of functions. The other parameters in this are that:

It creates a prompt asking the person if they want Fahrenheit -> Celsius or Celsius -> Fahrenheit

It then has another prompt asking what temperature they'd like converted

Finally, the output has both the original temperature and the converted one.

Also it was noted that I had to use IF statement to guide the "flow of the program." I've been having a lot of troubles with it, as I do not know how to create a program without the use of functions. I'm also having issues with figuring out how to program it so the program knows that the user picked Fahrenheit to Celsius/Celsius to Fahrenheit and prompts them about the temperature they'd want changed. Any help/guiding would be great. Thanks.

mrhoo
03-18-2012, 09:34 PM
prompt is a function.

Philip M
03-18-2012, 09:37 PM
Please look at forum rule 1.5. We will not do your homework for you, but if you make a substantial effort yourself and post your code here, then doubtless someone will help you correct/improve it.

mrhoo - I would have said that prompt() is a method, not a function. Note that some browsers may block prompt dialogs for security reasons.

There is no need for a function if the script is to run immediately on page load.

As a boon, I will start you off with the prompt which prompts repeatedly until F or c is entered:-


<script type = "text/javascript">

for (var i=1; i<2; i++) {
var conv = prompt("Type F to convert Farenheit to Celcius, C to convert Celcius to Fahrenheit", "");
if ((conv == null) || ((conv.toLowerCase() != "c") && (conv.toLowerCase() != "f"))) {
alert ("You must enter F or C!! ");
i -- ;
}
}
// now continue and prompt in a similar fashion for the temperture to be converted. Naturally the response must be a number, and furthermore a number within a sensible range (say -100F to 1000F). If not you must prompt the user again repeatedly until a (sensible) number is entered.

var temp = Number(prompt("Enter the temperature to be converted",""));
// remember that all values entered by the user are strings unless/until converted to numbers.


That is enough help at this stage. You should note that often validation of user input requires more code than the simple calculation.

"99.9 percent of lawyers give the rest a bad name." - Unknown

felgall
03-18-2012, 09:56 PM
Any properly written JavaScript will use at least one function since wrapping the entire code inside an anonymous function is the easiest way to remove your code from the global scope where it might inter fere with other scripts.

Also all functions in JavaScript are also methods since any that you don't explicitly define as belonging to a particular object will all be methods of the window object. If you were to reserve the term function to mean methods of the window object then prompt is a function because it is a method of the window object just as any functions you define yourself would be - except that prompt serves no useful purpose whereas functions you create yourself (or window methods if you prefer to call them that) will generally be there for a reason.

rnd me
03-19-2012, 05:28 PM
prompt is a function.

it's an object in IE...

rnd me
03-19-2012, 05:29 PM
Also all functions in JavaScript are also methods since any that you don't explicitly define as belonging to a particular object will all be methods of the window object.


function a(){
function b(a){return a*a;}
}
here, what is "b" a method of?

Philip M
03-19-2012, 05:36 PM
Most references to prompt() turned up in Google such as http://www.javascriptkit.com/jsref/window.shtmltells me that prompt() is a method of the window object.
http://javascript.about.com/library/bldialog.htm (felgall) describes it as a pre-defined function.
http://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm talks of predefined functions but then lists many methods such as parseInt(), replace() and length. (but not alert/confirm/prompt).
http://www.functionx.com/javascript/Lesson06.htm considers alert/confirm/prompt as built-in functions.
http://users.dickinson.edu/~braught/courses/cs131s99/Lessons/05-PreFunc.html ditto, along with Math.random() etc.

But I doubt if the OP cares a dime. :p Can anyone say how it matters?
If prompt() is classed as a function (which it seems it is as well as being a method), then his assignment is impossible.

c1lonewolf
03-19-2012, 10:22 PM
<script language="javascript">
var cvtype = prompt("Type F to convert Farenheit to Celcius, C to convert Celcius to Fahrenheit", "");

var ortemp = prompt("Enter temp to convert", "");

var cvm = (cvtype == c) ? 'c' : 'f';

//--- conversion coding goes here ---//

var cvtemp = ''; //created from above

var Output = '';

Output += ortemp + '<br>' + cvtemp

doument.write(Output);

</script>

back to the basics...;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum