Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: How to calculate Tax Code

1. 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>
<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>

<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>```

• Originally Posted by triko
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.

• Originally Posted by Philip M
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?

• 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.

• Originally Posted by felgall
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";
}
}

</script>```

• Originally Posted by Philip M
^ 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.

• Wait a moment guy!!!
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!

• Originally Posted by triko
Wait a moment guy!!!
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.

• 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?

• 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.

• Users who have thanked Old Pedant for this post:

triko (11-01-2012)

• 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

• 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.

• Originally Posted by Old Pedant
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);

</script>```

• 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.

• 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.

•
Page 1 of 2 12 Last

Posting Permissions

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