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

Join 105,763 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,690 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!



Javascript functions

2 Pages V  1 2 >  
Closed TopicStart new topic

Javascript functions, I need some pointers on creating Javascript functions

truetoon
post 13 Jun, 2008 - 05:31 AM
Post #1


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

I have managed to complete the problem from the first post but the project i am doing seems to get harder and more obscure the further i get. I have encountered a problem with creating functions and using them to convert values of an array when the array is written out.

CODE

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



var currentDate = 300;

var dateDue







function showCatalogue()

{

    for (var count = 0; count < bookArray.length; count = count + 1)

    
    {
    dateDue = dueArray[count] - currentDate
    document.write(count + '------' + bookArray[count] +'------' + daysOverDue(dueDate) + '<BR>');

    }

}

function daysOverdue(dateDue)
{
if (dateDue = 300)
{
document.write('On shelf')
}
}


i have created the above code to write out the arrays, i have only included the array i am having trouble with in the code. What i need to do is covert dueArray[count] into text based on the value. i.e if the due date equals 300 then it writes out 'on shelf' instead. The code above is probably a mile off. i have tried lots of variations but this was my last attempt.

any hints, pointers or assistance would be fantastically appreciated. smile.gif Thanks in advance
User is offlineProfile CardPM

Go to the top of the page


atdrago
post 13 Jun, 2008 - 07:54 AM
Post #2


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Your function actually will work with just a few corrections.

First, in your if statement, make it '==' instead of '='. If you keep it 'dateDue = 300' you're actually just setting 300 to dateDue.

Next, have your function return the string you want it to print out. That way, the function is actually a string equal to the returned value.

Here's how I'd do it:
javascript
function daysOverDue(dateDue)
{
if(dateDue == 300)
{
return("On shelf");
}
}


By the way, you don't need to declare dateDue as a global variable if you're passing it into different functions.
Right now this:
javascript
daysOverDue(dateDue);

will do the exact same thing as this:
javascript
daysOverDue();


Doing it the way you have it could lead to errors later. Just declare dateDue inside of showCatalogue().

Adam

I also just noticed in your showCatalogue() function you're calling daysOverdue(dueDate) instead of daysOverdue(dateDue).
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 13 Jun, 2008 - 08:35 AM
Post #3


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

Thanks Adam, i made the suggested changes but it still doesn't work here is the code now

CODE

function showCatalogue()

{

    for (var count = 0; count < bookArray.length; count = count + 1)

    {
    dateDue = dueArray[count] - currentDate
    document.write(count + '------' + bookArray[count] +'------' + daysOverdue(dateDue) + '<BR>');

    }

}

function daysOverdue(dateDue)
{
if (dateDue == 300)
{
return ('On shelf')
}
}

apologies if i am being thick smile.gif

This post has been edited by truetoon: 13 Jun, 2008 - 08:38 AM
User is offlineProfile CardPM

Go to the top of the page

atdrago
post 13 Jun, 2008 - 09:07 AM
Post #4


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Sorry, I should've added this. You have to declare your variable inside of ShowCatalogue(). Here's what it should look like:
javascript
function showCatalogue()
{
var dateDue;
for (var count = 0; count < bookArray.length; count = count + 1)
{
dateDue = dueArray[count] - currentDate
document.write(count + '------' + bookArray[count] +'------' + daysOverdue(dateDue) + '<BR>');
}

}


See if that works.
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 13 Jun, 2008 - 10:02 AM
Post #5


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

still no joy it just writes out 'undefined' for the due date??
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 13 Jun, 2008 - 11:06 AM
Post #6


#include <soul.h>

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



Thanked 32 times

Dream Kudos: 675
My Contributions


Okay, I think I got it working. YOu were having a lot of errors in the code. First off you forgot to place the bookArray in the code anywhere so it wasn't able to call to it in the functions. Then you also had the daysOverDue() functuion not returning anything (which is the rason it was always coming back undefined) additionally that was because you didn't have anything happen if the book was not "On Shelf". After that it was just the basics of trying to get all the variable names for the same thing to be spelt the same way. Here is the new (and mostly working)code:

CODE
var dueArray = new Array(0, 0, 345, 137, 0, 0, 0, 181, 0, 0, 0, 131, 284, 0, 0, 56, 315, 0, 0, 0);
var bookArray = new Array("SL", "JT", "Test", "Ajax", "Learning", "Other thing", "More stuff", "Teaching", "Girls", "Boys", "Poeple", "Computer", "JS", "PHP", "C", "C++", "C#", "Basic", "VB", "Perl");

var currentDate = 300;
var dateDue;

function showCatalogue(){
  for(count=0; count<bookArray.length; count++){
    dateDue = dueArray[count] - currentDate;
    document.write(count + '------' + bookArray[count] +'------' + daysOverDue(dateDue) + '<BR>');
  }
}
function daysOverDue(dateDue){
  if(dateDue == '300'){
    return "On shelf";
  }
  else{
    return "<font color='#ff0000'>" + dateDue + "</font>";
  }
}
User is offlineProfile CardPM

Go to the top of the page

atdrago
post 13 Jun, 2008 - 11:21 AM
Post #7


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


There's no bookArray in the code you posted.

Can I see all the code, please?

Thanks,

Adam
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 13 Jun, 2008 - 11:26 AM
Post #8


#include <soul.h>

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



Thanked 32 times

Dream Kudos: 675
My Contributions


Yes, there is:

QUOTE
var dueArray = new Array(0, 0, 345, 137, 0, 0, 0, 181, 0, 0, 0, 131, 284, 0, 0, 56, 315, 0, 0, 0);
var bookArray = new Array("SL", "JT", "Test", "Ajax", "Learning", "Other thing", "More stuff", "Teaching", "Girls", "Boys", "Poeple", "Computer", "JS", "PHP", "C", "C++", "C#", "Basic", "VB", "Perl");

var currentDate = 300;
var dateDue;

function showCatalogue(){
for(count=0; count<bookArray.length; count++){
dateDue = dueArray[count] - currentDate;
document.write(count + '------' + bookArray[count] +'------' + daysOverDue(dateDue) + '<BR>');
}
}
function daysOverDue(dateDue){
if(dateDue == '300'){
return "On shelf";
}
else{
return "<font color='#ff0000'>" + dateDue + "</font>";
}
}


Pretty sure that says bookArray...
User is offlineProfile CardPM

Go to the top of the page

atdrago
post 13 Jun, 2008 - 11:29 AM
Post #9


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


I changed the code and made it work with dueArray instead of bookArray. It's returning 'undefined' because dateDue will never be 300. Take a closer look at your code.

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

function showCatalogue()
{
var dateDue;
var count;

for (count = 0; count < dueArray.length; count = count + 1)
{
dateDue = dueArray[count] - currentDate;
document.write("" + count + "------" + dueArray[count] +"------" + daysOverdue(dateDue) + "<br />");
}

}
function daysOverdue(dateDue)
{
if (dateDue == 300)
{
return ('On shelf');
}
else
{
return ("not on shelf");
}
}


Hope that helped you out a little bit. Good luck.

Edit: I just saw what you wrote. Your first post does not contain the declaration for bookArray. It contains references to it, but not the actual declaration. Just change it back from what I changed.
User is offlineProfile CardPM

Go to the top of the page

truetoon
post 13 Jun, 2008 - 11:29 AM
Post #10


New D.I.C Head

*
Joined: 22 May, 2008
Posts: 19

sure Adam, this is the code so far with the help from betawar. Thanks BetaWar by the way. I changed the return values and removed an extra bracket that wasn't needed (i think)It is still not fully working but it is now calling the second function but it just writes 'Not on shelf' for each line??

CODE

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 = dueArray[count] - currentDate;
    document.write(count + '------' + bookArray[count] +'------' + daysOverDue(dateDue) + '<BR>');
  }
}

function daysOverDue(dateDue)

{
  if(dateDue == 300)
    {
            return "On shelf";
    }
          else
        {
            return "not on shelf"
        }
}



User is offlineProfile CardPM

Go to the top of the page

atdrago
post 13 Jun, 2008 - 11:32 AM
Post #11


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


Apologies.

I guess his code was in another thread?

Adam
User is offlineProfile CardPM

Go to the top of the page

atdrago
post 13 Jun, 2008 - 11:38 AM
Post #12


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


QUOTE(truetoon @ 13 Jun, 2008 - 11:29 AM) *
It is still not fully working but it is now calling the second function but it just writes 'Not on shelf' for each line??


Err, sorry let me explain a little better.

Let's take dueArray[0]. dueArray[0] is equal to 0. currentDate (which was equal to 300 before but now isn't in your code for some reason??) is then subtracted form dueArray[0] and assigned to dateDue. So dateDue is now equal to -300.

dueArray[0] - currentDate = -300
or
0 - 300 = -300

Do that calculation with every number in your array and you'll see that dateDue will never be equal to 300. And therefore will always print "not on shelf."

I hope that makes more sense. smile.gif
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Closed TopicStart new topic
Time is now: 8/21/08 01:48PM

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