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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript function needed

Status
Not open for further replies.

amlilola

Programmer
Feb 14, 2008
5
Hello everybody,
i have this javascript code and desperately looking to create a function so I don't have to copy/paste all the time... code goes like this...


// first loop if((sjedalo=="A01")&&(sjedala_selektirana[1]==0)&&(brojac<5))
{
if(sjedala_slobodna[1])
{
sjedala_selektirana[1]=1;
brojac++;
if (document.form2.s1.value == "")
document.form2.s1.value = "A01";
else if (document.form2.s2.value == "")
document.form2.s2.value = "A01";
else if (document.form2.s3.value == "")
document.form2.s3.value = "A01";
else if (document.form2.s4.value == "")
document.form2.s4.value = "A01";
else if (document.form2.s5.value == "")
document.form2.s5.value = "A01";
}


// second if loop
else if((sjedalo=="A02")&&(sjedala_selektirana[2]==0)&&(brojac<5))
{
if(sjedala_slobodna[2])
{
sjedala_selektirana[2]=1;
brojac++;
if (document.form2.s1.value == "")
document.form2.s1.value = "A02";
else if (document.form2.s2.value == "")
document.form2.s2.value = "A02";
else if (document.form2.s3.value == "")
document.form2.s3.value = "A02";
else if (document.form2.s4.value == "")
document.form2.s4.value = "A02";
else if (document.form2.s5.value == "")
document.form2.s5.value = "A02";
}
}


// 3. if loop
else if((sjedalo=="A03")&&(sjedala_selektirana[3]==0)&&(brojac<5))
{
if(sjedala_slobodna[3])
{
sjedala_selektirana[3]=1;
brojac++;
if (document.form2.s1.value == "")
document.form2.s1.value = "A03";
else if (document.form2.s2.value == "")
document.form2.s2.value = "A03";
else if (document.form2.s3.value == "")
document.form2.s3.value = "A03";
else if (document.form2.s4.value == "")
document.form2.s4.value = "A03";
else if (document.form2.s5.value == "")
document.form2.s5.value = "A03";
}
}


the code goes like that untill "A016" so I have
A01
A02
A03
...
A09
A010
A011
A012
A013
A014
A015
A016


Thanks...
 
What have you tried to date? I'd hate to suggest things you've already tried to do.

Let us know how you go,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
something like this:

function prva(sjedalo)
{
for(var i=0;i<17;i++)
{
var image = document.images['A' + lz(i)];
if((sjedalo==image)&&(sjedala_selektirana==0)&&(brojac<5))
{
if(sjedala_slobodna)
{
sjedala_selektirana=1;
brojac++;
if (document.form2.s1.value == "")
document.form2.s1.value = image;
else if (document.form2.s2.value == "")
document.form2.s2.value = image;
else if (document.form2.s3.value == "")
document.form2.s3.value = image;
else if (document.form2.s4.value == "")
document.form2.s4.value = image;
else if (document.form2.s5.value == "")
document.form2.s5.value = image;
}
}
}

function lz(n) { // prefix leading zero for numbers < 10

n = String(n);

n = '0' + n;

return n;

}
 
Ah. Gotcha. You were pretty close , I think. Here is one way to do it:
Code:
for(var i=[!]1[/!];i<17;i++) {
  var image = [!]'A'
  if (i<10) image += '0';
  image += i[/!];

And here is a less obvious solution (but with less code):
Code:
for(var i=[!]1[/!];i<17;i++) {
  var image = [!]'A' + (i<10?0:'') + i[/!];
You don't need the function lz() any longer.

Let us know how you go!
Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Still doesn't work.

I'm pretty much sure that everything works fine with the zeros and non-zeros, but not so sure about the other part of the code.

I'm new to javascript and this is pretty much the LAST part of the biggest project I've ever been on (online tickets reservation system built from starch) and it's driving me crazy :).

This script is about seats in cinema (they are green when available, red when occupied and yellow when clicked or mouseover) and I have cca 600 lines of code just for cinema with 16 seats (copy/paste model, no functions), now imagine what about cinema with 400+ seats. That's why I need fuuunction to do this :(.
 
You can check it out at
website is in Croatian (ulaznice=tickets) and it is a part of my computer science course at Faculty of Electrical Engineering, Mechanical Engineering and Naval Architecture
at University of Split, Croatia.

It is 99% complete (some make-up and details needed) but it works fine, now, I just need this f*** javascript function to make code more "user friendly" and optimized for big cinemas
:)
 
This is a function version of your first post:
Code:
function prva(sjedalo) {
   var j = 1;
   var obj;
   for (var i = 1; i < 17; i++) {
      var image = "A" + i.padString("0", 2);
      if (sjedalo == image && sjedala_selektirana[i] == 0 && brojac < 5) {
         if (sjedala_slobodna[i]) {
            sjedala_selektirana[i] = 1;
            brojac++;
            for (j = 1; j < 6; j++) {
               obj = document.forms["form2"].elements["s" + j]
               if (!obj.value.length) {
                  obj.value = image;
               }
            }
         }
      }
   }
}

Object.prototype.padString = function (padChar, padLength, padEnd) {
   var padString = this.toString();
   var padCharString = "";
   for (var padIterator = 0; padIterator < padLength - padString.length; padIterator++) {padCharString += padChar;}
   return (padEnd) ? padString + padCharString : padCharString + padString;
};

A few things to point out:

1) In your function example you posted, you are missing a closing bracket. You have 4 opening brackets ({) and only 3 closing brackets (})
2) The padString method is something I wrote a while back and use very often. It's handy for adding leading 0's to numbers - but can be used for lots of other purposes.
3) You never made any reference to the images collection in your first post, so I didn't bother putting that into my solution (even though it was added later)
4) "... and it is a part of my computer science course ..."
This will probably be the end of any more help you get on this post. If this is a homework assignment (even if it will be used productively) then asking for outside assistance is cheating. You should ask your teacher for help instead of us - that is what they are paid for. Normally I don't go through the bother of helping students, but I've already typed out all the code above before you mentioned that it was course work.
5) I think that mark was playing at the fact that you said your site is made from "starch".

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
ahhh... This is how it goes...

1) thnx for the "}" - I figured it's something like this (error) :)

2)adding leading zeros. I figured that I don't need that much, becouse every number (seat) starts with leading 0, so...

3)images go like this (A01, A02, A03... A015, A016) <- 16 seats for demo cinema

4) it's no homework, I used a wrong set of words. it is my project I started couple of months ago, and this year we had this "software engineering" subject where we had to organize a project (team leader, programmers, idea... etc), and we used e-tickets, only thing is, we REALLY had a true project to work with. Understand?

Well, never mind...

Everywhone who helpes, and any help would be great, will had his name writen on our website with link or whatever...

Thanks in advance...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top