Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a function please

Status
Not open for further replies.

vfattarej

Programmer
Jan 9, 2010
2
SE
Hi all Javascript pros. I'm quite new with javascript and i have a function here that i don't realy understand (from a book i have). Now my question is. Could somebody please explain the code to me part by part? And maybe show other ways to write the code? It would be very nice of you. If you need my mail then just ask, then you can send it on my email if you'd like. Here's the code.. And thank you .

function sistaefternamnen() {
var temp = "";
for (var i = 1; i < 4; i++) {
if ((registrerade-i) == 0 || i == 3 && (registrerade-i) >= 0) {
temp = temp + efternamn[registrerade-i];
}
else if ((registrerade-i) > 0) {
temp = temp + efternamn[registrerade-i] + ", ";
}
else {
break;
}
}
var tempP = document.getElementById('p3');
tempP.lastChild.nodeValue = temp;
}
 
I do believe its quite straight forward, though why its doing all that I do not know, but I'll try to explain what its doing:


Code:
[gray]
function sistaefternamnen() {
[green]//declare temp variable[/green]
var temp = "";
[green]// Initiate a 4 cycle for loop from i=1 to when i=4[/green]
for (var i = 1; i < 4; i++) {
[green]// check whether the result of subtracting the current value of 'i' from the value of the variable 'registrade' is equal to 0 (which would mean i equals registrade), or if i equals 3 and if the result of registrade minus i is greater or equal to zero   [/green]
if ((registrerade-i) == 0 || i == 3 && (registrerade-i) >= 0) {

[green]//If any of the above is true, then it sets the variable temp to the result of concatenating itself with the value of the array efternamn at the index that is equal to the value of registrade minus the current value of 'i' [/green]
temp = temp + efternamn[registrerade-i];
}

[green]// if the above was not true, but the result of registrade minus the current value of i is greater than zero,  then again it concatenates the value of temp to the value of the array in the specified index and adds a comma at the end of the string. [/green]
else if ((registrerade-i) > 0) {
temp = temp + efternamn[registrerade-i] + ", ";
}
[green]//if none of those conditions are true it just terminates the loop at that point and moves on.[/green]
else {
break;
}
}
[green]//once the loop is terminated either because it completed itselg 4 times or because it was terminated by the break statement above, it moves on, gets the object with an id of 'p3' into the variable 'tempP' and proceeds to assign the value of the variable 'temp' to the nodeValue (that is the last text string in that particular object) of the  last child element of the element with the ID of 'p3' .  This can be either nothing if none of the conditions above where true or variable mix of up to 4 values if more than 1 condition was true. and if it was true in more than one cycle of the for loop.    [/green]
var tempP = document.getElementById('p3');
tempP.lastChild.nodeValue = temp;
} 
[/gray]

With that said its hard to say if it can be rewritten since I don;t exactly know what its attempting to do. Because I have no idea what the p3 object is or what its last child is or why its trying to alter that specific string.

Perhaps if you explain to us what the example in the book is trying to accomplish we may be able to come up with a better approach.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for your answer. I got it a lot better now. I checked further in the book and after a couple of hundred pages the whole code was there. I will show it to you so you will better understand, It's quite long though so I'm realy sorry about that. And i realy appriciete your help:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"<html>
<head>
<title>Inlämningsuppgift 4</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">



var fornamn = new Array();

var efternamn = new Array();

var personnummer = new Array();

var ar = new Array();

var registrerade = 0;

var man = 0;

var kvinna = 0;

var aldst = 0;


function GO() {

if (rattsymboler()) {

ok();

helaaldern();
skrivutinformation();
sistaefternamnen();



}
}




function ok() {

fornamn[registrerade] = document.getElementById('FNAMN');

fornamn[registrerade] = fornamn[registrerade].value;

efternamn[registrerade] = document.getElementById('ENAMN');

efternamn[registrerade] = efternamn[registrerade].value;

personnummer[registrerade] = document.getElementById('PNR');
personnummer[registrerade] = personnummer[registrerade].value;

registrerade++;

var h1 = document.getElementById("h0");

h1.lastChild.nodeValue = fornamn[registrerade-1] + " , du är (id) år gammal ";
}


function kollapersonnummer() {

var personnummer = "";

personnummer = document.getElementById('PNR');

personnummer = personnummer.value;
if (kolladatum(personnummer) == true) {

if (kollaPlusEllerMinus(personnummer) == true) {

if (kollasymboler(personnummer) == true) {

return true;

}
}
}
}





function kollaPlusEllerMinus(personnummer) {

var number = personnummer;

var sign = personnummer.charAt(6);

if (sign == "-" || sign == "+") {

return true;

}
else {

alert("Nej nej nej, innan dina fyra sista siffror så måste du ha ett plus eller minus");
return false;

}
}






function kollasymboler(personnummer) {

var number = personnummer;

var code = "";

for (var i = 7; i <=10; i++) {

code = code + personnummer.charAt(i);

}
if (!(isNaN(code))) {

if ((code.charAt(2) % 2) == 1) {

man++;

else {
kvinna++;

}
return true;
}
else {
alert("Du har skrivit fel på dina sista fyra siffror");
return false;

}
}






function kolladatum(personnummer) {
var date = "";
var number = personnummer;
for (var i = 0; i <=5; i++) {
date = date + personnummer.charAt(i);
}
if (!(isNaN(date))) {
return true;
}
else {
alert("Du har skrivit fel på dina första sex siffror ");
return false;
}
}





function kollaar(temp) {
if (personnummer[registrerade-1].charAt(0) != 0) {
temp = (personnummer[registrerade-1].charAt(0) + personnummer[registrerade-1].charAt(1));
}
else {
temp = personnummer[registrerade-1].charAt(1);
}
return temp;
}






function skrivutinformation() {

skrivutalder();
var p0 = document.getElementById("p0");

p0.lastChild.nodeValue = "Antalet registrerade män: " + man;
//
var p1 = document.getElementById("p1");
p1.lastChild.nodeValue = "Antalet registrerade kvinnor: " + kvinna;
var p4 = document.getElementById("p4");

p4.lastChild.nodeValue = fornamn[aldst] + " " + efternamn[aldst] + " du är " + ar[aldst] + " år och äldst bland de registrerade.";

}







function skrivutalder() {
var temp = fornamn[registrerade-1] + ", du är " + ar[registrerade-1] + " år gammal. ";
if (ar[registrerade-1] == 17) {
temp = temp + "Du är snart vuxen!!!"

}
else if (ar[registrerade-1] >= 65) {
temp = temp + "Du är pensionär!!!";

}
else if (ar[registrerade-1] > 10 && ar[registrerade-1] < 13) {
temp = temp + "Du är snart tonåring!!!";
}
else {
temp = temp;
}
var h1 = document.getElementById("h0");
h1.lastChild.nodeValue = temp;
}






function sistaefternamnen() {
var temp = "";
for (var i = 1; i < 4; i++) {
if ((registrerade-i) == 0 || i == 3 && (registrerade-i) >= 0) {
temp = temp + efternamn[registrerade-i];
}
else if ((registrerade-i) > 0) {
temp = temp + efternamn[registrerade-i] + ", ";
}
else {
break;
}
}
var tempP = document.getElementById('p3');
tempP.lastChild.nodeValue = temp;
}





function artonhundratalet(temp) {
var tTemp = temp;
if (tTemp <= 9) {
ar[registrerade-1] = 100 + 9 - tTemp;
}
else{
ar[registrerade-1] = ((200-tTemp) + 9);
}
if (ar[registrerade-1] >= ar[aldst]) {
aldst = registrerade-1
}
}





function nittonhundratalet(temp) {
var tTemp = temp;
if (tTemp <= 9) {
ar[registrerade-1] = 9 - tTemp;
}
else{
ar[registrerade-1] = ((100-tTemp) + 9);
}
if (ar[registrerade-1] >= ar[aldst]) {
aldst = registrerade-1
}
}



function helaaldern() {
var temp = 0;
temp = kollaar(temp);
temp = parseInt(temp);
var sign = (personnummer[registrerade-1].charAt(6));
if (sign == "-") {

nittonhundratalet(temp);

}
else {
artonhundratalet(temp);

}
}




function rattsymboler() {
var temp = document.getElementById('FNAMN');

var temp2 = document.getElementById('ENAMN');

if ((isNaN(temp.value)) && (isNaN(temp2.value))) {

var temp3 = document.getElementById('PNR');

if (temp3.value.length == 11) {

if (kollapersonnummer()) {

return true;

}
}
else {
alert("Ditt personnummer är inte rätt!!!");
return false;

}
}
else {
alert("Ditt namn och/eller efternamn är felskrivet");
return false;

}
}




</script>
</head>
<body>
<h1>Registrering</h1>
<form action="#" method="post">
<fieldset>
<legend>Personuppgifter</legend>

<label for="FNAMN">Förnamn:</label>
<input type="text" id="FNAMN"><br>
<label for="ENAMN">Efternamn:</label>
<input type="text" id="ENAMN"><br>
<label for="PNR">Personnummer:</label>
<input type="text" id="PNR"><br>
<input type="button" id="sendButton" value="Spara" onclick="GO();">

</fieldset>
</form>
<h1 id="h0">...</h1>
<p id="p0">Antalet registrerade män: 0</p>
<p id="p1">Antalet registrerade kvinnor: 0</p>
<p id="p2">De tre senaste registrerade heter i efternamn: </p>
<p id="p3">ingen registrerad</p>

<p id="p4">Det finns ingen registrerad.</p>
</body>
</html>
 
My Swedish is not that good, but form what I gather its just checking the 'personnummer' to be correct and then calculating the age.

Then changes the value of the paragraph to show a sentence with the name and other data.

Since I can't read the Swedish much, and I have no idea how to properly format thepersonnummer I wouldn't want to attempt to actually change the code.

I can however suggests you look into using the innerHTML property of the Paragraph objects instead going through the nodes. Much easier to read and understand.


Perhaps someone who can read Swedish better than I can, can be of more help.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top