Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,165 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,485 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



function with 2 arguments

2 Pages V  1 2 >  
Reply to this topicStart new topic

function with 2 arguments, calculate a fine

baggins
post 16 Jun, 2008 - 04:36 AM
Post #1


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Hi everyone,

New question, new headache blink.gif

I have a list of books which I have added an overdue date, no problem so far. But now I need to put a fine if the overdue pass a certain date.

If more than 200 days overdue, user pay full replacement. If less 200 days and if it worth more than £50, the user is fine 2% of the replacement cost for everyday the book is overdue. And if the overdue is less than 200 days but under £50, user is fined 1% of the replacement cost for every day that the book is overdue.

The function should take 2 arguments, the number of book and the number of days overdue.

My coding for the overdue date is ok, but how do I organise myself for the catalogue combine the overdue date with the levy fine, and calculate the fine? I don't know where to start blink.gif

CODE


document.write('<BR><BR>BARSETSHIRE COUNTY LIBRARIES<BR>');
document.write('==============================<BR>');
document.write('<BR>Administrator interface - Showing catalogue<BR><BR>');
showCatalogue();
var currentDate = 300;


var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon', 'Life of Tristram Shandy 1st ed', 'Remembrance of Things Past', 'On Her Majesty\'s Secret Service', 'The Wind-up Bird Chronicle', 'Last Exit to Birmingham', 'Love in the Time of Cholera', 'Java for Dummies 2nd Ed', 'The French Revolution', 'She Married a Duke', 'The Works of Shakespeare', 'A Tale of Two Cities', 'The Tailor of Gloucester', 'The Diary of a Nobody', 'The A to Z of Loving'];


var dueArray = [0, 0, 345, 137, 0, 0, 0, 181, 0, 0, 0, 131, 284, 0, 0, 56, 315,0, 0, 0];

function showCatalogue(){
  for(count=0; count < bookArray.length; count++){
    dateDue = currentDate - dueArray[count];
    document.write(count + '------' + bookArray[count] +'------' + daysOverDue(dateDue) + '<BR>');
  }
}

function daysOverDue(dateDue)

{
  if(dateDue < 300)
    {
            return ("overdue by" + ' ' + dateDue + ' ' + 'days!');
    }
    else(dateDue > 300)
    {
            return ("on shelf");
    }
}
//below is how I start with the function levyFine
var replacementCostArray = [10.99, 3.87, 5.99, 11.99, 15.45, 3.99, 250.0, 58.70, 4.99, 9.99, 7.99, 7.99, 12.50, 75.0, 3.45, 15.0, 12.99, 5.50, 7.99, 6.99];
var fineRate = 1/100;

function levyFine(replacementCost)

{
  if ((replacementCost == 6) && (replacementCost == 7))
     {
           return ("Fine of" + ' ' + '£' + replacementCostArray + fineRate);
     }
}
//I'm confused how to combine levyFine and dateDue


smile.gif

User is offlineProfile CardPM

Go to the top of the page


truetoon
post 16 Jun, 2008 - 05:01 AM
Post #2


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 24

Hi baggins, i am doing the same course and also had trouble with this. I have sorted it now. Without giving you my code (because we may get in trouble) i will attempt to give a couple of pointers.

Because the dueDate is currentDate - dueArray[count] the values you are looking for in the functions are < 0 <200 and == 300.

In the levy fine you can simply use the dateDue variable as long as you declare it somewhere. So the levyFine is 1 or 2% replacement cost times the dateDue.

it may look something like this

CODE


function levyFine()
{
if (dateDue < 200 && replacementCostArray[count] > 50)
{
return  2% replacement cost times the dateDue.//let you work this bit out!!


like i said i don't want to give too much away but this may help a little

This post has been edited by truetoon: 16 Jun, 2008 - 05:03 AM
User is offlineProfile CardPM

Go to the top of the page

baggins
post 16 Jun, 2008 - 08:12 AM
Post #3


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Thanks Truetoon,

Your explanation was perfect, I just figure it out.

icon_up.gif biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

baggins
post 16 Jun, 2008 - 08:50 AM
Post #4


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Apparently we are a lot on the same course smile.gif
User is offlineProfile CardPM

Go to the top of the page

cavvysri
post 17 Jun, 2008 - 12:00 PM
Post #5


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions


QUOTE(baggins @ 16 Jun, 2008 - 08:50 AM) *

Apparently we are a lot on the same course smile.gif

im on the same course too lol

i have done this question, but for some reason the books that are supposed to be on loan, come up with a fine of -2 for example..not sure where ive gone wrong for this to happen..

any ideas?
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 18 Jun, 2008 - 05:58 AM
Post #6


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 24

QUOTE(cavvysri @ 17 Jun, 2008 - 12:00 PM) *

QUOTE(baggins @ 16 Jun, 2008 - 08:50 AM) *

Apparently we are a lot on the same course smile.gif

im on the same course too lol

i have done this question, but for some reason the books that are supposed to be on loan, come up with a fine of -2 for example..not sure where ive gone wrong for this to happen..

any ideas?


hi you need to write into your function the code that handles books that have a dateDue that is less than 0

if(dateDue <0)
write out 'on loan'

this is a bit general but post the code you have created so far i might be able to help further.
User is offlineProfile CardPM

Go to the top of the page

baggins
post 18 Jun, 2008 - 09:13 AM
Post #7


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Hello everyone,

The end of the tunnel smile.gif last problem! crazy.gif

I have to show the fines of the reader, with function showFines but no arguments in it. Now is been two days on it. I simply call my function and put document write, and should add together but no it does not work like that apparently.

CODE


document.write('==============================<BR>');
document.write('<BR>Administrator interface - Showing fines<BR><BR>');
document.write('<BR>');
document.write('<BR>');

showFines();

function showFines()

{
document.write('Reader' + ' ' + 'ly-44' + ' ' + 'is fined' + ' ' + '£' + dateDue + fineDue + '<BR>');    
}
                

</SCRIPT>

</HEAD>

<BODY>
</BODY>



Any ideas?
smile.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 18 Jun, 2008 - 10:52 PM
Post #8


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,540



Thanked 61 times

Dream Kudos: 925
My Contributions


Printing/writing things out in js (and other languages) is interesting. One basic rule of thumb is if you are adding a number and a string you get a string like 5+'5' will output 55 but 5+5 will output 10.

So, when adding 2 numbers and a string you need to change things around to something like (5+5) + '5' (outputting 105)

Here is some code that should work (assuming that all the variables can be accessed:

CODE
function showFines(){
document.write('Reader' + ' ' + 'ly-44' + ' ' + 'is fined' + ' ' + '£' + (dateDue + fineDue) + '<BR>');    
}


That is of course assuming you wanted dataDue and fineDue to be added together instead of outputted next to eachother.
User is online!Profile CardPM

Go to the top of the page

baggins
post 19 Jun, 2008 - 05:06 AM
Post #9


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



Nop, doesn't work.

That's what appear on the screen:
Reader rs-14 is fined on shelf
Reader rs-14 is fined on shelf
Reader rs-14 is fined overdue by -45 days!Fine of £0.0599
Reader rs-14 is fined overdue by 163 days!Fine of £0.1199
Reader rs-14 is fined on shelf
Reader rs-14 is fined on shelf

I don't know why it write on shelf blink.gif ; that line appears 6 time, cause I put in 'for' statement: readerNameArray, and there is 6 names. It suppose to write the total of fines of reader rs-14, instead it wrote only 2. Maybe my statement is wrong, but I can't use arguments.

Here is part of my code:

CODE


document.write('==============================<BR>');
document.write('<BR>Administrator interface - Showing fines<BR><BR>');
document.write('<BR>');
document.write('<BR>');


showFines();

function showFines()

{
for(count= 0; count < readerNameArray.length; count = count + 1)
  {
    dateDue = currentDate - dueArray[count];
    fineDue = dateDue + replacementCostArray[count];
    document.write('Reader' + ' ' + readerNameArray[2] + ' ' + 'is fined' + ' ' + (daysOverDue(dateDue) + levyFine(numberBook,dateDue)) + '<BR>');
  }
}

User is offlineProfile CardPM

Go to the top of the page

cavvysri
post 20 Jun, 2008 - 12:34 PM
Post #10


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions


QUOTE(baggins @ 19 Jun, 2008 - 05:06 AM) *

Nop, doesn't work.

That's what appear on the screen:
Reader rs-14 is fined on shelf
Reader rs-14 is fined on shelf
Reader rs-14 is fined overdue by -45 days!Fine of £0.0599
Reader rs-14 is fined overdue by 163 days!Fine of £0.1199
Reader rs-14 is fined on shelf
Reader rs-14 is fined on shelf

I don't know why it write on shelf blink.gif ; that line appears 6 time, cause I put in 'for' statement: readerNameArray, and there is 6 names. It suppose to write the total of fines of reader rs-14, instead it wrote only 2. Maybe my statement is wrong, but I can't use arguments.

Here is part of my code:

CODE


document.write('==============================<BR>');
document.write('<BR>Administrator interface - Showing fines<BR><BR>');
document.write('<BR>');
document.write('<BR>');


showFines();

function showFines()

{
for(count= 0; count < readerNameArray.length; count = count + 1)
  {
    dateDue = currentDate - dueArray[count];
    fineDue = dateDue + replacementCostArray[count];
    document.write('Reader' + ' ' + readerNameArray[2] + ' ' + 'is fined' + ' ' + (daysOverDue(dateDue) + levyFine(numberBook,dateDue)) + '<BR>');
  }
}



hi baggins, im stuck on this piece of code too...all mine seems to print out is reader undefined etc..

its very annoying
User is offlineProfile CardPM

Go to the top of the page

nebuna
post 22 Jun, 2008 - 08:52 AM
Post #11


New D.I.C Head

*
Joined: 17 Jun, 2008
Posts: 4

Hi,

did anyone sorted this out? I'm going mad, here's my code, seems to be everything ok but still not working:

CODE
for(index=0; index < bookArray.length; index++)
{
    dateDue = currentDate - dueArray[index];
}
document.write('<BR><BR>BARSETSHIRE COUNTY LIBRARIES<BR>');
document.write('==============================');
document.write('<BR>Administrator interface - Showing catalogue<BR>');
showCatalogue();

function showCatalogue()
{
  for(index = 0; index < bookArray.length; index++)
{
    dateDue = currentDate - dueArray[index];
document.write(index + '------' + bookArray[index] +'------' +
               levyFine(dateDue) + '<BR>');
}
}

function levyFine(dateDue)

{
  if(dateDue < 0)
  {
      return ("On loan");
  }
else
{
        if(dateDue == 300)
        {
           return ("On shelf");
        }
   else
   {
       if(dateDue < 200 && replacementCostArray[index] > 50);
       {
          return (((replacementCostArray[index]*2/100)*dateDue));
       }

     else
     {
       if(dateDue < 200 && replacementCostArray[index] < 50)
       {
            return (((replacementCostArray[index]*2/100)*dateDue));
       }
       else(dateDue > 200);
       {
         return (replacementCostArray[index]);
       }
     }
   }
}
}
User is offlineProfile CardPM

Go to the top of the page

nebuna
post 22 Jun, 2008 - 09:17 AM
Post #12


New D.I.C Head

*
Joined: 17 Jun, 2008
Posts: 4

QUOTE(nebuna @ 22 Jun, 2008 - 08:52 AM) *

Hi,

did anyone sorted this out? I'm going mad, here's my code, seems to be everything ok but still not working:

CODE
for(index=0; index < bookArray.length; index++)
{
    dateDue = currentDate - dueArray[index];
}
document.write('<BR><BR>BARSETSHIRE COUNTY LIBRARIES<BR>');
document.write('==============================');
document.write('<BR>Administrator interface - Showing catalogue<BR>');
showCatalogue();

function showCatalogue()
{
  for(index = 0; index < bookArray.length; index++)
{
    dateDue = currentDate - dueArray[index];
document.write(index + '------' + bookArray[index] +'------' +
               levyFine(dateDue) + '<BR>');
}
}

function levyFine(dateDue)

{
  if(dateDue < 0)
  {
      return ("On loan");
  }
else
{
        if(dateDue == 300)
        {
           return ("On shelf");
        }
   else
   {
       if(dateDue < 200 && replacementCostArray[index] > 50);
       {
          return (((replacementCostArray[index]*2/100)*dateDue));
       }

     else
     {
       if(dateDue < 200 && replacementCostArray[index] < 50)
       {
            return (((replacementCostArray[index]*2/100)*dateDue));
       }
       else(dateDue > 200);
       {
         return (replacementCostArray[index]);
       }
     }
   }
}
}



Now sorted, just few semicolons not in the place..
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/6/08 11:14AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month