Javascript functions

I need some pointers on creating Javascript functions

Page 1 of 1

13 Replies - 1435 Views - Last Post: 13 June 2008 - 11:43 AM

#1 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Javascript functions

Post icon  Posted 13 June 2008 - 05:31 AM

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.

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. :) Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript functions

#2 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 07:54 AM

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:
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:
daysOverDue(dateDue);

will do the exact same thing as this:
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).
Was This Post Helpful? 0
  • +
  • -

#3 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Javascript functions

Posted 13 June 2008 - 08:35 AM

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

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 :)

This post has been edited by truetoon: 13 June 2008 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 09:07 AM

Sorry, I should've added this. You have to declare your variable inside of ShowCatalogue(). Here's what it should look like:
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.
Was This Post Helpful? 0
  • +
  • -

#5 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Javascript functions

Posted 13 June 2008 - 10:02 AM

still no joy it just writes out 'undefined' for the due date??
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Javascript functions

Posted 13 June 2008 - 11:06 AM

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:

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>";
  }
}

Was This Post Helpful? 0
  • +
  • -

#7 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 11:21 AM

There's no bookArray in the code you posted.

Can I see all the code, please?

Thanks,

Adam
Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Javascript functions

Posted 13 June 2008 - 11:26 AM

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...
Was This Post Helpful? 1
  • +
  • -

#9 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 11:29 AM

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.

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.
Was This Post Helpful? 1
  • +
  • -

#10 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Javascript functions

Posted 13 June 2008 - 11:29 AM

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

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" 
 		}
}


Was This Post Helpful? 0
  • +
  • -

#11 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 11:32 AM

Apologies.

I guess his code was in another thread?

Adam
Was This Post Helpful? 0
  • +
  • -

#12 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: Javascript functions

Posted 13 June 2008 - 11:38 AM

View Posttruetoon, on 13 Jun, 2008 - 11:29 AM, said:

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. :)
Was This Post Helpful? 0
  • +
  • -

#13 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,148
  • Joined: 07-September 06

Re: Javascript functions

Posted 13 June 2008 - 11:41 AM

That is because for the moment the way the logic is going says that dueDate[count] - currentDate must be equal to 300 (making dueDate[count] needing to be 600) for it to be on shelf. I am not quite sure what the dueDate variables are using as specification of time, (is it equivalent to days?, minutets?, seconds?, hours?, weeks? etc.) If I knew what the timeframe was I may be able to make the code closer to what you are looking for.

Do you want 0 to mean the book is onshelf (overDue would be 0) or do you infact want it to be 300 means on shelf?

Sorry, but a little clarification could make this go a lot faster.
Was This Post Helpful? 0
  • +
  • -

#14 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Javascript functions

Posted 13 June 2008 - 11:43 AM

thanks for all your help guys it is really appreciated. Maybe if i stick at this i will be able to return the favour one day???? :P I have just solved the problem i should written this
function daysOverdue(dateDue)
{
if (dateDue == -300)
{



it is now functions how i want it to. I just hope i can take this knowledge and use it for the rest of my project!!!

thanks again

This post has been edited by truetoon: 13 June 2008 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1