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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to calculate Tax Code

    Hi all, today Teacher give me a problem: Tax Code
    But when I arrive at the function for calculate the three letters of name es: JOHN = JHN; I don't know how to do

    Code:
    <!DOCTYPE HTML>
        <html>
            <head>
                <title>Codice Fiscale</title>
               
                   <script type="text/javascript">
                 
                //Here have the functions by Var, that have took by Input
                
                function calculateName (n);
                {
                    
                }
                
                function calculateSurname (s);
                {
                        
                }
                
                function calculateMunicipal (m);
                {
                    
                }
                
                function calculateDay (d);
                {
                    
                }
                
                function calculateMonth (m);
                {
                    
                }
                
                function calculateYear (y);
                {
                    
                }
                
                function calculatePinControl (p);
                {
                    
                }
                
                function calculation ()
                {
                    //create var, for take a data to input
                    var name1 =      document.getElementById("name).value;
                    var surname1 =   document.getElementById("surname").value;
                    var municipal1 = document.getElementById("municipal").value;
            //TODO: var sex1 =       document.getElementById("male" + "female").value;
                    var day1 =       document.getElementById("day").value;
                    var month1 =     document.getElementById("month").value;
                    var year1 =      document.getElementById("year").value;
                    
                    //With value of var I do the calculation of Tax Code
                    var txName = calculateName (name1);
                    var txSurname = calculateSurname (surname1);
                    var txMunicipal = calculateMunicipal (municipal1);
                    var txDay = calculateDate (day1, sex1);
                    var txMonth = calculateMonth (month1);
                    var txYear = calculateYear (year1);
                    var txTotal = name1 + surname1 + municipal1 + day1 + month1 + year1;
                    var pinControl = calculatePinControl (txTotal);
                    var tx = txTotal + pinControl;
                    return tx;
                    
                    
                    
                }
                </script>
               
            </head>
            
            <body>
                
                
                
                Name:    <input type = "text" id = "name" /> <br />
                Surname: <input type = "text" id = "surname" /> <br />
                Municipal:  <input type = "text" id = "municipal" /> <br />
                Sex:   <input type = "radio" id = "male" name = "radioSex" value = "M" checked = "true">Male</input>
                         <input type = "radio" id = "female" name = "radioSex" value = "F" checked = "false">Female</input>
                Day:  <input type = "text" id = "day" />
                Month:    <input type = "text" id = "month" />
                Year:    <input type = "text" id = "year" />
                         <button type = "button" onclick = "calculation ()"> CONFIRM </button>
                
                
            </body>
        </html>
    Last edited by VIPStephan; 10-29-2012 at 05:34 PM. Reason: added code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by triko View Post
    But when I arrive at the function for calculate the three letters of name es: JOHN = JHN; I don't know how to do
    Sorry, I do not know what you mean. How can you 'calculate' a name? Are you trying to extract the first three consonants of the name?

    var sex1 = document.getElementById("male" + "female").value;
    That is not the way to get the value of a checked radio button.

    You will need to include a lot of validation to check that the user inputs are valuid/sensible. It is very unclear what you mean by "day", "month" and "year". Do you mean Date Of Birth? What if the user enters 56 February 2099?

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



    Quizmaster: In Russian literature, the author of Dr. Zhivago was Boris who?
    Contestant: Karloff.
    Last edited by Philip M; 10-29-2012 at 05:36 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.

  • #3
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Sorry, I do not know what you mean. How can you 'calculate' a name? Are you trying to extract the first three consonants of the name?

    var sex1 = document.getElementById("male" + "female").value;
    That is not the way to get the value of a checked radio button.

    You will need to include a lot of validation to check that the user inputs are valuid/sensible. It is very unclear what you mean by "day", "month" and "year". Do you mean Date Of Birth? What if the user enters 56 February 2099?

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



    Quizmaster: In Russian literature, the author of Dr. Zhivago was Boris who?
    Contestant: Karloff.
    An ok!!!
    So for day month years, Yes, is the born date!!!
    But my principal problem is how to extract by VAR name ( That I write in input type= "text") the consonants. Example: nicola = NCL, and stamp at screen NCL, after extract by surname, rossi = RSS. Understand more now?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    The simplest way to convert the name to all uppercase dropping all the vowels would be:

    Code:
    txName = name1.replace(/[^aeiou]/ig,'').toUpperCase();
    Of course you'd need to have covered regular expressions properly (either in the course or on your own) in order to be able to explain how that statement works.

    Without using a regular expression you'd need to use a loop that processes each character of the name separately and only adds it to the txname if it isn't a vowel.
    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by felgall View Post
    The simplest way to convert the name to all uppercase dropping all the vowels would be:

    Code:
    txName = name1.replace(/[^aeiou]/ig,'').toUpperCase();
    Of course you'd need to have covered regular expressions properly (either in the course or on your own) in order to be able to explain how that statement works.

    Without using a regular expression you'd need to use a loop that processes each character of the name separately and only adds it to the txname if it isn't a vowel.
    ^ means "not" so your regex removes the consonants, not the vowels.

    The flaw with that (if corrected) is that if the name starts with AEIOU it will be removed.
    So Allen becomes LLN, as does Ellen. Presumably that is not desired.

    Also what if the name has fewer than 3 consonants? E.g. Foot. Or consists entirely of vowels - Eau (= river in Lincolnshire)

    A more complex script is required to deal with these issues. The first letter of the name is preserved even if it is a vowel. If the code has fewer then 3 characters it is padded with X's to make a total of three.

    Code:
    <script type = "text/javascript">
    
    // convert name to three-letter code using consonants but preserving initial letter
    
    var cname = "Felgall";
    var first = cname.substring(0,1);
    var second = cname.substring(1);
    second =  second.replace(/[aeiou]/gi,"");
    var final = (first+second).toUpperCase().substring(0,3);
    if (final.length <3) {
    for (var i=3; i>=final.length; i--) {
    final += "X";
    }
    }
    alert (final);  // FLG
    
    </script>
    Last edited by Philip M; 10-29-2012 at 11:08 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.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by Philip M View Post
    ^ means "not" so your regex removes the consonants, not the vowels.
    Oops. That's what happens when you copy a piece of code from somewhere else and don't quite update it properly.
    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.

  • #7
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Wait a moment guy!!!
    I explained bad!
    So, this problem is the first that teacher give for homework! So isn't use the variable or algorithm tricky.
    I easy find 3 letters of a name and surname; Ex. Nicola Rossi: nclrss; Andrea Dalla Costa = ndrdll; Anna Grandis = nnxgrn; If in the name or surname miss the third letters, the program insert an X.
    <script>
    function
    {
    ??? // Here have algorithm that calculate the three letters
    }
    </script>
    <body>
    <input type ="name" /> // Here insert name that going to function
    </body>

    Now it's more Clear? Sorry for my bad English!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by triko View Post
    Wait a moment guy!!!
    I explained bad!
    So, this problem is the first that teacher give for homework! So isn't use the variable or algorithm tricky.
    I easy find 3 letters of a name and surname; Ex. Nicola Rossi: nclrss; Andrea Dalla Costa = ndrdll; Anna Grandis = nnxgrn; If in the name or surname miss the third letters, the program insert an X.
    <script>
    function
    {
    ??? // Here have algorithm that calculate the three letters
    }
    </script>
    <body>
    <input type ="name" /> // Here insert name that going to function
    </body>

    Now it's more Clear? Sorry for my bad English!
    No, it is not at all clear. I gave you a script to do exactly that in post #5. What was wrong with that?

    Note that there is a limit to the amount of homework we will do for you. We will correct/improve code that you yourslf have written but we will not do the whole thing for you. As you seem to be having problems with the first hurdle you may find this assignment rather tough going.
    Last edited by Philip M; 10-30-2012 at 06:45 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.

  • #9
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok!
    But, can you tell me what mean the:

    (/[aeiou]/gi,"");

    I have tested! The script work, but if the surname have A like first letters. How I do?

    Thanks for your time Philip

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    So you do *NOT* want to keep the first letter if it is a vowel?

    Then it's even easier:
    Code:
    function mungeName( name )
    {
        return ( name.replace(/[aeiou]/ig,"").toLowerCase() + "xxx" ).substring(0,3);
    }
    You see it? First, the replace removes all vowels. Then we convert what is left to lower case. Then we append three "x" characters, just in case. Then we take only the first three characters.

    If you'd like to see it work in pieces:
    Code:
    function mungeName( name )
    {
        var result = name.replace(/[aeiou]/ig,"");
        result = result.toLowerCase();
        result = result + "xxx" 
        result = result.substring(0,3);
        return result;
    }
    BUT ...

    But if this is the first homework assignment for this class, your teacher will NEVER believe that you came up with that answer by yourself. That's way beyond a first assignment level coding.
    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:

    triko (11-01-2012)

  • #11
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, I see, it's more trickly for me!!!
    But Work magically!!!
    So we have study at the moment the :
    JS Data Types
    JS Switch
    JS getElementByID
    and other bases of JS
    JS Loop For
    JS Loop While
    And these we have started to study the last lesson
    JS String
    JS Array
    And we have to create a program with this element

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    So you need to figure out a way to do the same thing WITHOUT using a regular expression.

    Hint: Try looking at String.charAt() method. And maybe String.indexOf( ) method.
    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.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    So you need to figure out a way to do the same thing WITHOUT using a regular expression.

    Hint: Try looking at String.charAt() method. And maybe String.indexOf( ) method.
    Here you are. Now you must do the rest for yourself.

    Code:
    <script type = "text/javascript">
    
    var cname = "qwertyuiop";
    cname = cname.toLowerCase();
    var newcname = "";
    for (var i =0; i<cname.length; i++) {
    if ((cname.charAt(i) != "e") && (cname.charAt(i) != "i")  && (cname.charAt(i) != "o") && (cname.charAt(i) != "o") && (cname.charAt(i) != "u")  )  {
    newcname += cname.charAt(i);
    }
    }
    newcname = (newcname.toUpperCase() + "XXX").substring (0,3);
    alert (newcname);
    
    </script>

    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.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Okay, Philip. I suggested String.indexOf so he could do this:
    Code:
    function mungeName( name )
    {
        name =  name.toLowerCase();
        var vowels = "aeiou";
        var newname = "";
    
        for ( var c = 0; c < name.length; ++c )
        {
            var ch = name.charAt(c);
            if ( vowels.indexOf(ch) < 0 ) { newname += ch; }
        }
        newname += "xxx";
        return newname.substring( 0, 3 );
    }
    I'm pretty sure he said he wanted an all lower case answer, by the by.
    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.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Once more, the cat .......

    He said

    But when I arrive at the function for calculate the three letters of name es: JOHN = JHN; I don't know how to do

    So I understood he wanted upper case.

    But then he said

    I easy find 3 letters of a name and surname; Ex. Nicola Rossi: nclrss; Andrea Dalla Costa = ndrdll; Anna Grandis = nnxgrn;

    Now it lower case with both first name and last name.

    It makes it harder when the OP is himself unclear what he is trying to do.

    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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