Jump to content

Trying to create an auto fixture update for a website im working on


dgenxp

Recommended Posts

Hi I am new to javascript and am wondering why this code does not return the value for which I need, Basically on the home page of a website I am developing there is a section that states when and where the next game is on the fixture. So what I am hoping to achieve is that this code will detect today's date and compare it with the next date in the Array that follows the current date, as an examplescan through the array of days, then compare this to the month if it is above today's date then the next game date is displayed along with the string that states the team that we are playing. when it hits the next date to stop checking until I reload the websitethe code that I wrote to support this is below, as I said I am new to javascript coding and if there is a better way then I will be more than happy to make the adjustments needed, I am not sure how to make an array of dates so I used a really botched version so i'm hoping that you guys can help me

var Fixtures = ["The Raiders V Valley Cougars","The Raiders V Leicester Storm","The Raiders V Torfaen Tigers","The Raiders V Bristol Sonics","The Raiders V Gloucestershire Warriors","The Raiders V Sheffield Hallam Eagles","The Raiders V Nottingham Outlaws","The Raiders V Leicester Storm","The Raiders V Coventry Bears","The Raiders V Oxford Cavaliers"				];var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var months = ["January","February","March","April","May","June","July","August","September","October","November""December"];var NowDate = new Date();	var HomeGameDays = [21,9,16,20,27,18,25,8,22,5];	var HomeGameMonths =[2 ,4, 4, 5, 5, 6, 6,7, 7,8]; 		var DateComparer = new Date();		var NextGameDate =  new Date();	var CaughtDate = false;		var Fixture ="Awaiting";var Passed =  "Pass";		for(i = 0; i < HomeGameMonths.length; i++)		{							if(NowDate.getMonth() <= HomeGameMonths[i])			{				if(NowDate.getDate() <= HomeGameDays[i] && CaughtDate == false){Passed = "Confirmation" + i;DateComparer = Date(2015,HomeGameMonths[i],HomeGameDays[i]); Fixture = Fixtures[i];NextGameDate = Date(2015, HomeGameMonths[i], HomeGameDays[i]);CaughtDate = true;}}}var NextGame = "Next> " + DateComparer;document.getElementById("demo").innerHTML = Fixture+ "</br>"+NextGameDate;</script>

Alot of this code has test elements in it just for my own debug purposes but the result that i get from this is

Awaiting

Wed Jul 29 2015 16:54:26 GMT+0100(GMT Daylight Time)

 

Obviously this is completely wrong as it should state

 

The Raiders V Oxford Cavaliers

Sat, August, 8, 2015

 

 

 

or something along those lines, instead it is completely ignoring the if statement and it is not adjusting the date at all, I am at a loss i have been programming for a long time now so this should be a simple piece of code but it has proven fruitless. I know there are other ways.. Better ways but as im new i used this method, if anybody can either tell me what is causing this problem or a better way to code it im all ears.

 

Thanks in advance

Edited by dgenxp
Link to comment
Share on other sites

instead it is completely ignoring the if statement

It's not ignoring anything. It's doing exactly what you told it to do, you just didn't tell it to do the right thing. If you start thinking in terms of the computer not working right then you're not going to be very good at debugging. You can add console.log statements in there or set breakpoints to step through the code so you can see exactly what it's doing, you can check the values that it's comparing to verify that it is in fact doing exactly what you told it to do, and you may also notice why it's not working the way you want.Instead of a bunch of parallel arrays that all hold related information, you should use a single array of objects. Instead of separate months and days just use date objects. e.g.:
var games = [  {    description: "The Raiders V Valley Cougars",    date: new Date(2015, 1, 21)  },  {     description: "The Raiders V Leicester Storm",    date: new Date(2015, 3, 9)  }  ...];
Now all of the related information is together in one place. You can loop through that and check games.date to compare the date with today, and since you're just comparing 2 date objects it's a whole lot easier. In your loop, once you find the first game that is after today then you save that game (you can save the entire games object) and then you can use break to stop the loop at that point. So, you loop until you find any game after today and then stop the loop and display that game.
Link to comment
Share on other sites

Ahh ok I get it, as per usual i'm over complicating things [FACEPALM] thanks for the help I was hoping there would be a way simular to this I think im on the right track now maybe me and javascript will get on after all, any other help would be appreciated but I think im good now

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...