Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-29-2012, 03:39 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 10-29-2012, 05:28 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 10-29-2012 at 05:36 PM..
Philip M is offline   Reply With Quote
Old 10-29-2012, 08:25 PM   PM User | #3
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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?
triko is offline   Reply With Quote
Old 10-29-2012, 09:53 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 10-29-2012, 10:44 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
__________________

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.

Last edited by Philip M; 10-29-2012 at 11:08 PM..
Philip M is offline   Reply With Quote
Old 10-30-2012, 02:06 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 10-30-2012, 06:17 PM   PM User | #7
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!
triko is offline   Reply With Quote
Old 10-30-2012, 06:43 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 10-30-2012 at 06:45 PM..
Philip M is offline   Reply With Quote
Old 10-30-2012, 07:44 PM   PM User | #9
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 10-30-2012, 08:04 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
triko (11-01-2012)
Old 10-30-2012, 08:12 PM   PM User | #11
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 10-30-2012, 08:36 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-30-2012, 09:14 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 10-30-2012, 11:14 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-31-2012, 08:34 AM   PM User | #15
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.