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