www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Simple problem: how to get an array 'recognized' by my other functions?

  1. #1
    Join Date
    Jun 2012
    Location
    FL, USA
    Posts
    48

    resolved [RESOLVED] Simple problem: how to get an array 'recognized' by my other functions?

    I've got some javascript here to calculate running totals in hours of time logged in <select> elements.

    Don't worry about figuring the code-- I know it works. My problem is that I want the totals calculated and this requires the use of an array from the OTHER function...

    Code:
    // -----------------FOR TIME LOG----------------------------
    
    // arrays needed
    timeins = new Array();
    timeins_split = new Array();
    timeins_min = new Array();
    timeins_dec = new Array();
    timeouts = new Array();
    timeouts_split = new Array();
    timeouts_min = new Array();
    timeouts_dec = new Array();
    totaltimes = new Array();
    dec_total = new Array();
    
    // make the onchange events
    
    for (y = 1; y <= 4; y++)
    {	
    	document.getElementById('timein' + y).onchange = addTimesIN;
    	document.getElementById('timeout' + y).onchange = addTimesOUT;
    }
    
    function addTimesIN()
    {
    	// set variables when onchange is triggered
    	timeins[y] = this.options[this.selectedIndex].value;
    	
    	// split the variables into mathematically workable stuff
    	timeins_split[y] = timeins[y].split(":");
    	
    	// re-assemble as decimals
    	timeins_min[y] = timeins_split[y][1] / 60; //works...
    	timeins_dec[y] = (parseInt(timeins_split[y][0]) + timeins_min[y]);
    	console.log('Time in: ' + timeins_dec[y]);
    	
    		// calculate that day's total
    		dec_total[1] = (parseInt(timeouts_dec[1]) - parseInt(timeins_dec[1]));
    		
    		// set it to the totals box
    		document.getElementById('totaltime1').value = dec_total[1];
    		
    		// add all totals
    		var all_time_totals = dec_total[1] + dec_total[2] + dec_total[3] + dec_total[4];
    		document.getElementById('alltimetotal').value = all_time_totals;
    		
    }
    
    function addTimesOUT()
    {
    	// set variables when onchange is triggered
    	timeouts[y] = this.options[this.selectedIndex].value;
    	
    	// split the variables into mathematically workable stuff
    	timeouts_split[y] = timeouts[y].split(":");
    	
    	// re-assemble as decimals
    	timeouts_min[y] = timeouts_split[y][1] / 60;
    	timeouts_dec[y] = (parseInt(timeouts_split[y][0]) + timeouts_min[y]);
    	console.log('Time Out: ' + timeouts_dec[y]);
    	
    		// calculate that day's total
    		dec_total[1] = (parseInt(timeouts_dec[1]) - parseInt(timeins_dec[1]));
    		
    		// set it to the totals box
    		document.getElementById('totaltime1').value = dec_total[1];
    		
    		// add all totals
    		var all_time_totals = dec_total[1] + dec_total[2] + dec_total[3] + dec_total[4];
    		document.getElementById('alltimetotal').value = all_time_totals;
    }
    
    
    // ------------------END TIME LOG--------------
    So in addTimesIN() i use timeins_dec[]
    and in addTimesOUT() i use timeouts_dec[]

    but because they are in separate functions those arrays aren't being recognized....

  2. #2
    Join Date
    Jun 2012
    Location
    FL, USA
    Posts
    48
    I found the fix. Combined it all in one function and re-defined the variables. Hope it helps somebody

    Code:
    // -----------------FOR TIME LOG----------------------------
    
    // arrays needed
    timeins = new Array();
    timeins_split = new Array();
    timeins_min = new Array();
    timeins_dec = new Array();
    timeouts = new Array();
    timeouts_split = new Array();
    timeouts_min = new Array();
    timeouts_dec = new Array();
    totaltimes = new Array();
    dec_total = new Array();
    // make the onchange events
    
    for (y = 1; y <= 4; y++)
    {	
    	document.getElementById('timein' + y).onchange = addTimes;
    	document.getElementById('timeout' + y).onchange = addTimes;
    }
    
    function addTimes()
    {
    	for (y = 1; y <= 4; y++)
    	{
    	// set variables when onchange is triggered
    	timeins[y] = document.getElementById('timein' + y).options[document.getElementById('timein' + y).selectedIndex].value;
    	timeouts[y] = document.getElementById('timeout' + y).options[document.getElementById('timeout' + y).selectedIndex].value;
    	
    	// split the variables into mathematically workable stuff
    	timeins_split[y] = timeins[y].split(":");
    	timeouts_split[y] = timeouts[y].split(":");
    	
    	// re-assemble as decimals
    	timeins_min[y] = timeins_split[y][1] / 60; //works...
    	timeins_dec[y] = (parseInt(timeins_split[y][0]) + timeins_min[y]);
    	
    	timeouts_min[y] = timeouts_split[y][1] / 60;
    	timeouts_dec[y] = (parseInt(timeouts_split[y][0]) + timeouts_min[y]);
    	
    		// calculate that day's total
    		dec_total[y] = (timeouts_dec[y] - timeins_dec[y]);
    		
    		// set it to the totals box
    		if (isNaN(dec_total[y]))
    			{dec_total[y] = 0;}
    		document.getElementById('totaltime' + y).value = dec_total[y];
    		
    		// add all totals
    		var all_time_totals = dec_total[1] + dec_total[2] + dec_total[3] + dec_total[4];
    		document.getElementById('alltimetotal').value = all_time_totals;
    	}
    		
    }
    
    // ------------------END TIME LOG--------------

  3. #3
    Join Date
    Nov 2010
    Posts
    1,084
    nvm...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles