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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating a program without functions

    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.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    prompt is a function.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    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:-

    Code:
    <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
    Last edited by Philip M; 03-18-2012 at 09:57 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    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.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by mrhoo View Post
    prompt is a function.
    it's an object in IE...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by felgall View Post
    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.
    Code:
    function a(){
        function b(a){return a*a;}
     }
    here, what is "b" a method of?
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    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/javasc..._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/...5-PreFunc.html ditto, along with Math.random() etc.

    But I doubt if the OP cares a dime. 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.
    Last edited by Philip M; 03-19-2012 at 06:06 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Posts
    462
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Code:
    <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...
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    For projects using MediaTypes (MIMETypes) visit E-BAM.net -(updated weekly)

    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.


  •  

    Posting Permissions

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