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 13 of 13
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to use a java function

    how do i rerun a part of code with out retyping it every time ?
    there must be some sort of simpler way so you can keep the script shorter
    Last edited by red566; 03-02-2013 at 09:04 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by red566 View Post
    how do i rerun a part of code with out retyping it every time ?
    there must be some sort of simpler way so you can keep the script shorter
    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.


    I am not sure that I understand you, but you should save your code in an .html file which you can run repeatedly in your browser.

    Code:
    <html>
    <head>
    </head>
    <body>
    
    MY SIMPLE WEB PAGE
    
    <script type = "text/javascript">
    
    alert ("Hello World");
    
    </script>
    
    </body>
    </html>

    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.

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.


    I am not sure that I understand you, but you should save your code in an .html file which you can run repeatedly in your browser.

    Code:
    <html>
    <head>
    </head>
    <body>
    
    MY SIMPLE WEB PAGE
    
    <script type = "text/javascript">
    
    alert ("Hello World");
    
    </script>
    
    </body>
    </html>
    sorry but i did say i was a newb and i dont know what i shod call it unless i just do the same as the first line

    what i am trying to do is make a "simple" text game and how i have it is by NO means the best way so dont worry about any other things but what i am asking and i know i will run in to problems later on
    BUT
    what i have so far is everything pops up as a prompt and i want the users input to be saved as lets say var A
    A is then checked against stuff like all the ways you say yes eg yes, Yes, y and so on and if it is the same as one of them it saves a "1" or a "0" as lets say var B
    then it has a IF to check if var is a 1 or a 0 and continuous the game down what ever path

    so what i want to happen is to some how recall? (sorry i dont know how to word it) that part of the script every time a yes or no thing is asked

    therefore i dont need add lots of copes of it where ever needed and my script is smaller and less confusing


    i was thinking as some how saving the hole var A yes checking thing as a var but that did not seem to work




    also i am having another problem where i saved a text that just told you what your stats are as a var but where it displayed all of the var in that text just read undefined but it worked fine as document.write (yes i did change all of the different things eg <br/> and \n to work)

    if you want i can post my script so far it is 164 lines so far but it has a lot of spacing and stuff in it and right now it also has some "unimplemented" stuff and things like that

    thanks

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    prompt() boxes are quite obsolete. Use a better method of obtaining user input.

    By all means post your code, but only if stripped down to the relevant code.

    To identify the user's response:-

    Code:
    var ans = prompt("Do you want to continue? (Y/N)","");
    ans = ans.toLowerCase();  // make lower case
    if (ans.substring(0,1) == "y") {
    // do whatever is required if the user responds "y", "Y", "yes", "YES" and so on.
    var b = 1;
    }
    else {
    var b = 0;
    }
    But I get the impression that you are trying to run before you can walk, or even crawl. If you don't know simple stuff like the above then I don't see how you can create a worthwhile game. And you seem not too familiar with the proper vocabulary, which makes it harder to guide you. Javascript is not just child's play!
    Last edited by Philip M; 03-01-2013 at 11:03 AM.

    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.

  • #5
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    prompt() boxes are quite obsolete. Use a better method of obtaining user input.

    By all means post your code, but only if stripped down to the relevant code.

    To identify the user's response:-

    Code:
    var ans = prompt("Do you want to continue? (Y/N)","");
    ans = ans.toLowerCase();  // make lower case
    if (ans.substring(0,1) == "y") {
    // do whatever is required if the user responds "y", "Y", "yes", "YES" and so on.
    var b = 1;
    }
    else {
    var b = 0;
    }
    But I get the impression that you are trying to run before you can walk, or even crawl. If you don't know simple stuff like the above then I don't see how you can create a worthwhile game. And you seem not too familiar with the proper vocabulary, which makes it harder to guide you. Javascript is not just child's play!
    i know all that stuff... apart form the
    "ans = ans.toLowerCase(); // make lower case" bit
    and yes i know i am trying to get ahead of my self
    but say i wanted to rerun the
    "ans = ans.toLowerCase(); // make lower case
    if (ans.substring(0,1) == "y") {
    // do whatever is required if the user responds "y", "Y", "yes", "YES" and so on."
    part later on is there any way of running it again with out retyping it is my question here

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    I don't think any of us understand why you would *EVER* "retype" it.

    You type it into a program editor (Notepad, Notepad+, whatever you use), save it as an ".html" file, and then run the HTML file in your browser.

    WHY would you ever ever need to "retype" it?

    Unless you mean that you want to use the same question and answer processing more than one place in the same page/program?
    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.

  • #7
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't think any of us understand why you would *EVER* "retype" it.

    You type it into a program editor (Notepad, Notepad+, whatever you use), save it as an ".html" file, and then run the HTML file in your browser.

    WHY would you ever ever need to "retype" it?

    Unless you mean that you want to use the same question and answer processing more than one place in the same page/program?
    well yes i would think that would be obvious ?

    need to run it a LOT of times over where needed

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Okay, so make it a function:
    Code:
    function getYesOrNoAnswer( msg )
    {
        while ( true ) /* loop forever until you get a valid response */
        {
            var ans = prompt( msg );
            // get first non-space character in lower case    
            ans = ans.replace(/\s/g,"").toLowerCase().charAt(0); 
            // and ignore all answers that don't start with a "y" or "n"
            if ( ans === "y" || ans === "n" ) return ans;  
        } 
    }
    var isAdult = getYesOrNoAnswer( "Are you over 18 years of age?" );
    var isSmoker = getYesOrNoAnswer( "Do you smoke?" );
    var isDrinker = getYesOrNoAnswer( "Do you drink alcohol?" );
    ... etc. ...
    BUT ... As you have been told, you should not be using prompt( ) except perhaps in student exercises given you by instructors who are at least 6 years behind the times in web development techniques.

    prompt( ) is considered VERY VERY OBSOLETE and should *NEVER* be used on a *REAL* website. Never.

    And alert( ) is almost as bad. You should only be using it for maybe debugging your code and, possibly, when you get a really fatal error and don't know what else to do (though even then there are better things to use). And the same applies to document.write( ).

    If you are using any of those three--and if your instructor or your book is telling you to use any of them--then it is time to find another instructor or book.
    Last edited by Old Pedant; 03-02-2013 at 02:44 AM.
    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.

  • #9
    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 red566 View Post
    how do i rerun a part of code with out retyping it every time ?
    there must be some sort of simpler way so you can keep the script shorter
    There is - it is called a function.

    You should have been introduced to functions in about the first or second lesson on JavaScript - long before lesson fifty where document.write, prompt and alert are mentioned as things to get rid of if you are updating antiquated scripts.

    http://javascriptexample.net/basics15.php
    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.

  • #10
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    There is - it is called a function.

    You should have been introduced to functions in about the first or second lesson on JavaScript - long before lesson fifty where document.write, prompt and alert are mentioned as things to get rid of if you are updating antiquated scripts.

    http://javascriptexample.net/basics15.php
    well that is just sad cos i am now on lesson 4 for java and you have just listed the first 3 days the other was math stuff
    the class is only 1 day a week to that why i am here

    a function looks to be just what i need but i still have no clue how to use it even after reading that link and googleing for a bit
    can you give me an example with a yes/no input thing pleas it would be a grate help thanks

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by red566 View Post
    well that is just sad cos i am now on lesson 4 for java and you have just listed the first 3 days the other was math stuff
    the class is only 1 day a week to that why i am here

    a function looks to be just what i need but i still have no clue how to use it even after reading that link and googleing for a bit
    can you give me an example with a yes/no input thing pleas it would be a grate help thanks
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    Old Pedant has very clealy shown you how to use a function "with a yes/no input thing". What more do you want? Have you looked at
    http://www.w3schools.com/js/js_functions.asp

    You really must try to improve your language skills. If you cannot distinguish between the words "grate" and "great" or "hole" and "whole" you are likely run into significant problems in later life. You seem to be weak on the use of capital letters as well. The personal pronoun I is capitalised in English. So is the first letter of a sentence. If that is simple sloppiness then again you need to be aware that all computer programming requires great accuracy. Javascript is case-sensitive (and so am I).

    I am not being nasty. But you should be aware that any potential employer will dump a CV which is mis-spelled or badly punctuated to the bottom of the pile. You should try and eliminate bad habits now before they become ingrained. Look at it like this - poor spelling/lack of punctuation may cost you many thousands of dollars in reduced salary over your lifetime.

    You have not changed your thread title as requested - another reason why a potential employer would probably reject you.
    Last edited by Philip M; 03-02-2013 at 11:09 AM.

    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.

  • #12
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    Old Pedant has very clealy shown you how to use a function "with a yes/no input thing". What more do you want? Have you looked at
    http://www.w3schools.com/js/js_functions.asp

    You really must try to improve your language skills. If you cannot distinguish between the words "grate" and "great" or "hole" and "whole" you are likely run into significant problems in later life. You seem to be weak on the use of capital letters as well. The personal pronoun I is capitalised in English. So is the first letter of a sentence. If that is simple sloppiness then again you need to be aware that all computer programming requires great accuracy. Javascript is case-sensitive (and so am I).

    I am not being nasty. But you should be aware that any potential employer will dump a CV which is mis-spelled or badly punctuated to the bottom of the pile. You should try and eliminate bad habits now before they become ingrained. Look at it like this - poor spelling/lack of punctuation may cost you many thousands of dollars in reduced salary over your lifetime.

    You have not changed your thread title as requested - another reason why a potential employer would probably reject you.
    That looks a LOT more helpful thanks

    Also i am dyslexic so you dont even wanna know what my spelling looks like with out spell check

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by red566 View Post
    That looks a LOT more helpful thanks

    Also i am dyslexic so you dont even wanna know what my spelling looks like with out spell check
    If that is really true (and some very eminent education gurus are sceptical that dyslexia really exists) then I am sorry to say that your employment prospects are seriously impaired. You should not shelter behind an excuse but try harder to get your spelling right. Many who are diagnosed as dyslexic use it as an excuse for not having taken the time to check their spelling. "wanna" is not a word of the English language.


    http://hitchensblog.mailonsunday.co....yslexia_e.html
    Last edited by Philip M; 03-03-2013 at 01:19 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.


  •  

    Posting Permissions

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