Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    I'm having trouble looping over an array.

    I want to provide others with a type of javascript calculator that shows the number of BUSINESS days between two points in time. I am VERY good at figuring that out on my own and wrote a perl script that makes a master list of the business dates (yes, it even knows how to skip over Good Friday and Easter); But I'm trying to help others who have no programming skills with this problem.

    I want to place the array of dates into a javascript tool for others to measure the exact number of business days between two points in time LOCALLY on their computer, without using CGI. They should be able to just load the page, select a begin date, (I know how to code the start date selection just fine) and set the number of "X" business days, and the calculator spits out the correct date by iterating over the array and finding the index value corresponding to start date plus "X" further down the array.

    Here's the array for 2017 in my prototype code:

    Code:
    new Array(10417,10517,10617,10917,11017,11117,11217,11317,11717,11817,11917,12017,12317,12417,12517,12617,12717,13017,13117,20117,20217,20317,20617,20717,20817,20917,21017,21317,21417,21517,21617,21717,22117,22217,22317,22417,22717,22817,30117,30217,30317,30617,30717,30817,30917,31017,31317,31417,31517,31617,31717,32017,32117,32217,32317,32417,32717,32817,32917,33017,33117,40317,40417,40517,40617,40717,41017,41117,41217,41317,41717,41817,41917,42017,42117,42417,42517,42617,42717,42817,50117,50217,50317,50417,50517,50817,50917,51017,51117,51217,51517,51617,51717,51817,51917,52217,52317,52417,52517,52617,53017,53117,60117,60217,60517,60617,60717,60817,60917,61217,61317,61417,61517,61617,61917,62017,62117,62217,62317,62617,62717,62817,62917,63017,70317,70517,70617,70717,71017,71117,71217,71317,71417,71717,71817,71917,72017,72117,72417,72517,72617,72717,72817,73117,80117,80217,80317,80417,80717,80817,80917,81017,81117,81417,81517,81617,81717,81817,82117,82217,82317,82417,82517,82817,82917,83017,83117,90117,90517,90617,90717,90817,91117,91217,91317,91417,91517,91817,91917,92017,92117,92217,92517,92617,92717,92817,92917,100217,100317,100417,100517,100617,100917,101017,101117,101217,101317,101617,101717,101817,101917,102017,102317,102417,102517,102617,102717,103017,103117,110117,110217,110317,110617,110717,110817,110917,111017,111317,111417,111517,111617,111717,112017,112117,112217,112417,112717,112817,112917,113017,120117,120417,120517,120617,120717,120817,121117,121217,121317,121417,121517,121817,121917,122017,122117,122217,122617,122717,122817,122917);
    
    var index;
    var counting;
    	window.confirm("I'm ready to begin looping...");
    for (index = 0; index < 255; ++index) {
       	if(document.forms[0].BeginDate.value == Array[index]){
    		window.confirm("I matched the start date...");
                    var matched++;
    		}
       	if(matched){
                    counting++;
    		}
       	if(counting == document.forms[0].DayCount.value){
    		window.confirm("The end date is:"+Array[index]);
    		}
    	}
    But I'm clearly not doing something right, because I never get a confirmation window for the end date. The loop is clearly where I'm off track. If i remove the loop part of the code, the "ready to begin looping" window shows up just fine. With the loop as shown, no alerts will show up.

    I look forward to the inevitable dope-slap for whatever obvious mistakes I'm making; I just need to learn this part of Javascripting better. -SC45

  2. #2
    New to the CF scene
    Join Date
    Aug 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    One thing I'm realizing-- I DON'T need to iterate beyond the start date; I should just use MATH operations to go DIRECTLY to the answer:


    Code:
    new Array(10417,10517,10617,10917,11017,11117,11217,11317,11717,11817,11917,12017,12317,12417,12517,12617,12717,13017,13117,20117,20217,20317,20617,20717,20817,20917,21017,21317,21417,21517,21617,21717,22117,22217,22317,22417,22717,22817,30117,30217,30317,30617,30717,30817,30917,31017,31317,31417,31517,31617,31717,32017,32117,32217,32317,32417,32717,32817,32917,33017,33117,40317,40417,40517,40617,40717,41017,41117,41217,41317,41717,41817,41917,42017,42117,42417,42517,42617,42717,42817,50117,50217,50317,50417,50517,50817,50917,51017,51117,51217,51517,51617,51717,51817,51917,52217,52317,52417,52517,52617,53017,53117,60117,60217,60517,60617,60717,60817,60917,61217,61317,61417,61517,61617,61917,62017,62117,62217,62317,62617,62717,62817,62917,63017,70317,70517,70617,70717,71017,71117,71217,71317,71417,71717,71817,71917,72017,72117,72417,72517,72617,72717,72817,73117,80117,80217,80317,80417,80717,80817,80917,81017,81117,81417,81517,81617,81717,81817,82117,82217,82317,82417,82517,82817,82917,83017,83117,90117,90517,90617,90717,90817,91117,91217,91317,91417,91517,91817,91917,92017,92117,92217,92517,92617,92717,92817,92917,100217,100317,100417,100517,100617,100917,101017,101117,101217,101317,101617,101717,101817,101917,102017,102317,102417,102517,102617,102717,103017,103117,110117,110217,110317,110617,110717,110817,110917,111017,111317,111417,111517,111617,111717,112017,112117,112217,112417,112717,112817,112917,113017,120117,120417,120517,120617,120717,120817,121117,121217,121317,121417,121517,121817,121917,122017,122117,122217,122617,122717,122817,122917);
    
    var index;
    var counting;
    	window.confirm("I'm ready to begin looping...");
    for (index = 0; index < 255; ++index) {
       	if(document.forms[0].BeginDate.value == Array[index]){
    		window.confirm("I matched the start date... and the END Date is: "+Array[index+document.forms[0].BeginDate.value]);
                    var matched++;
    		}
    	}

  3. #3
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    West Des Moines, Iowa
    Posts
    8,104
    Thanks
    37
    Thanked 1,086 Times in 1,082 Posts
    So is there a question here? did you work it out? Is Good Friday a day off in your business?
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  4. #4
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,505
    Thanks
    4
    Thanked 503 Times in 491 Posts
    Do you actually HAVE 255 array indexes? Why are you looping a fixed amount instead of dynamically checking for the LENGTH of the array?

    you also declare "new Array' but dont' assign it to a variable, meaning your access of Array[i] inside the loop is pure gibberish. NOT that we say "new Array" anymore as the [] shorthand is just simpler/easier.

    Would also help if your code indentation made something remotely resembling SENSE... and if you have multiple VAR in a row, don't waste time saying VAR on all of them. Comma delimiting is your friend.

    ALSO not sure why you're wasting time on a pre-increment... making matched a new variable erases the existing value for... who knows what... and there's really no reason for matched to even BE a separate variable much less one you would increment.

    I'm guessing WILDLY here, but:

    Code:
    var
    	data = [
    		10417, 10517, 10617, 10917, 11017, 11117, 11217, 11317, 11717, 11817,
    		11917, 12017, 12317, 12417, 12517, 12617, 12717, 13017, 13117, 20117,
    		20217, 20317, 20617, 20717, 20817, 20917, 21017, 21317, 21417, 21517,
    		21617, 21717, 22117, 22217, 22317, 22417, 22717, 22817, 30117, 30217,
    		30317, 30617, 30717, 30817, 30917, 31017, 31317, 31417, 31517, 31617,
    		31717, 32017, 32117, 32217, 32317, 32417, 32717, 32817, 32917, 33017,
    		33117, 40317, 40417, 40517, 40617, 40717, 41017, 41117, 41217, 41317,
    		41717, 41817, 41917, 42017, 42117, 42417, 42517, 42617, 42717, 42817,
    		50117, 50217, 50317, 50417, 50517, 50817, 50917, 51017, 51117, 51217,
    		51517, 51617, 51717, 51817, 51917, 52217, 52317, 52417, 52517, 52617,
    		53017, 53117, 60117, 60217, 60517, 60617, 60717, 60817, 60917, 61217,
    		61317, 61417, 61517, 61617, 61917, 62017, 62117, 62217, 62317, 62617,
    		62717, 62817, 62917, 63017, 70317, 70517, 70617, 70717, 71017, 71117,
    		71217, 71317, 71417, 71717, 71817, 71917, 72017, 72117, 72417, 72517,
    		72617, 72717, 72817, 73117, 80117, 80217, 80317, 80417, 80717, 80817,
    		80917, 81017, 81117, 81417, 81517, 81617, 81717, 81817, 82117, 82217,
    		82317, 82417, 82517, 82817, 82917, 83017, 83117, 90117, 90517, 90617,
    		90717, 90817, 91117, 91217, 91317, 91417, 91517, 91817, 91917, 92017,
    		92117, 92217, 92517, 92617, 92717, 92817, 92917, 100217, 100317,
    		100417, 100517, 100617, 100917, 101017, 101117, 101217, 101317,
    		101617, 101717, 101817, 101917, 102017, 102317, 102417, 102517,
    		102617, 102717, 103017, 103117, 110117, 110217, 110317, 110617,
    		110717, 110817, 110917, 111017, 111317, 111417, 111517, 111617,
    		111717, 112017, 112117, 112217, 112417, 112717, 112817, 112917,
    		113017, 120117, 120417, 120517, 120617, 120717, 120817, 121117,
    		121217, 121317, 121417, 121517, 121817, 121917, 122017, 122117,
    		122217, 122617, 122717, 122817, 122917
    	],
    	counting = 0,
    	incCount = 0;
    	
    window.confirm("I'm ready to begin looping...");
    
    for (var i = 0, j = data.length; i < j; i++) {
    	if (document.forms[0].BeginDate.value == data[i]) {
    		window.confirm("I matched the start date...");
    		incCount = 1;
    	}
    	counting += incCount;
    	if (counting == document.forms[0].DayCount.value) {
    		window.confirm("The end date is:" + data[i]);
    		break; // short circuit out of loop, we're DONE!
    	}
    }
    Though I'm wondering why you'd be looping through the arrays instead of just using indexOf. (though you'd need to polyfill for older browsers, that's easy enough)... at least to find the start.

    Also pretty sure your array values are NOT compatible with a <input type="date"> value.
    Last edited by deathshadow; Aug 14th, 2017 at 11:57 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. Users who have thanked deathshadow for this post:

    SandCastle45 (Aug 15th, 2017)

  6. #5
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,505
    Thanks
    4
    Thanked 503 Times in 491 Posts
    Yeah, skip that looping nonsense:

    Code:
    var
    	data = [
    		10417, 10517, 10617, 10917, 11017, 11117, 11217, 11317, 11717, 11817,
    		11917, 12017, 12317, 12417, 12517, 12617, 12717, 13017, 13117, 20117,
    		20217, 20317, 20617, 20717, 20817, 20917, 21017, 21317, 21417, 21517,
    		21617, 21717, 22117, 22217, 22317, 22417, 22717, 22817, 30117, 30217,
    		30317, 30617, 30717, 30817, 30917, 31017, 31317, 31417, 31517, 31617,
    		31717, 32017, 32117, 32217, 32317, 32417, 32717, 32817, 32917, 33017,
    		33117, 40317, 40417, 40517, 40617, 40717, 41017, 41117, 41217, 41317,
    		41717, 41817, 41917, 42017, 42117, 42417, 42517, 42617, 42717, 42817,
    		50117, 50217, 50317, 50417, 50517, 50817, 50917, 51017, 51117, 51217,
    		51517, 51617, 51717, 51817, 51917, 52217, 52317, 52417, 52517, 52617,
    		53017, 53117, 60117, 60217, 60517, 60617, 60717, 60817, 60917, 61217,
    		61317, 61417, 61517, 61617, 61917, 62017, 62117, 62217, 62317, 62617,
    		62717, 62817, 62917, 63017, 70317, 70517, 70617, 70717, 71017, 71117,
    		71217, 71317, 71417, 71717, 71817, 71917, 72017, 72117, 72417, 72517,
    		72617, 72717, 72817, 73117, 80117, 80217, 80317, 80417, 80717, 80817,
    		80917, 81017, 81117, 81417, 81517, 81617, 81717, 81817, 82117, 82217,
    		82317, 82417, 82517, 82817, 82917, 83017, 83117, 90117, 90517, 90617,
    		90717, 90817, 91117, 91217, 91317, 91417, 91517, 91817, 91917, 92017,
    		92117, 92217, 92517, 92617, 92717, 92817, 92917, 100217, 100317,
    		100417, 100517, 100617, 100917, 101017, 101117, 101217, 101317,
    		101617, 101717, 101817, 101917, 102017, 102317, 102417, 102517,
    		102617, 102717, 103017, 103117, 110117, 110217, 110317, 110617,
    		110717, 110817, 110917, 111017, 111317, 111417, 111517, 111617,
    		111717, 112017, 112117, 112217, 112417, 112717, 112817, 112917,
    		113017, 120117, 120417, 120517, 120617, 120717, 120817, 121117,
    		121217, 121317, 121417, 121517, 121817, 121917, 122017, 122117,
    		122217, 122617, 122717, 122817, 122917
    	],
    	dateStart,
    	dateEnd;
    	
    window.confirm('Looking up date info');
    
    dateStart = data.indexOf(document.forms[0].BeginDate.value);
    if (dateStart >= 0) {
    	window.confirm('Start date found: ' + data[dateStart]);
    	dateEnd = dateStart + Number(document.forms[0].DayCount.value);
    	if (dateEnd < data.length) {
    		window.confirm('End date found: ' + data[dateEnd]);
    	} else window.confirm('End date not found!');
    } else window.confirm('Start Date Not Found!');
    I force the number typecast since JavaScript has the NASTY habit of forcing string additions thanks to the mental-midgetry that is loose typecasting.

    Mind you for IE8/earlier you'd need to polyfill Array.indexOf -- check MDN theirs isn't too horrible. My elementals.js library also provides a polyfill for most ECMAScript 6 Array and String additions.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  7. #6
    New to the CF scene
    Join Date
    Aug 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well, the question WAS, why do I get a value of undefined when I try to check Array[index]?

    Sometimes the answer is just too obvious. Since arrays are OBJECTS, you are supposed to give them a name:


    Code:
    var DateArray = new Array(10417,10517,10617,10917,11017,11117,11217,11317,11717,11817,11917,12017,12317,12417,12517,12617,12717,13017,13117,20117,20217,20317,20617,20717,20817,20917,21017,21317,21417,21517,21617,21717,22117,22217,22317,22417,22717,22817,30117,30217,30317,30617,30717,30817,30917,31017,31317,31417,31517,31617,31717,32017,32117,32217,32317,32417,32717,32817,32917,33017,33117,40317,40417,40517,40617,40717,41017,41117,41217,41317,41717,41817,41917,42017,42117,42417,42517,42617,42717,42817,50117,50217,50317,50417,50517,50817,50917,51017,51117,51217,51517,51617,51717,51817,51917,52217,52317,52417,52517,52617,53017,53117,60117,60217,60517,60617,60717,60817,60917,61217,61317,61417,61517,61617,61917,62017,62117,62217,62317,62617,62717,62817,62917,63017,70317,70517,70617,70717,71017,71117,71217,71317,71417,71717,71817,71917,72017,72117,72417,72517,72617,72717,72817,73117,80117,80217,80317,80417,80717,80817,80917,81017,81117,81417,81517,81617,81717,81817,82117,82217,82317,82417,82517,82817,82917,83017,83117,90117,90517,90617,90717,90817,91117,91217,91317,91417,91517,91817,91917,92017,92117,92217,92517,92617,92717,92817,92917,100217,100317,100417,100517,100617,100917,101017,101117,101217,101317,101617,101717,101817,101917,102017,102317,102417,102517,102617,102717,103017,103117,110117,110217,110317,110617,110717,110817,110917,111017,111317,111417,111517,111617,111717,112017,112117,112217,112417,112717,112817,112917,113017,120117,120417,120517,120617,120717,120817,121117,121217,121317,121417,121517,121817,121917,122017,122117,122217,122617,122717,122817,122917);
    
    var index;
    var counting;
    	window.confirm("I'm ready to begin looping...");
    var arraycount=DateArray.length-1;
    for (index = 0; index < arraycount; ++index) {
       	if(document.forms[0].BeginDate.value == DateArray[index]){
    		window.confirm("I matched the start date... and the END Date is: "+DateArray[index+document.forms[0].DayCount.value]);  // a DIFFERENT mistake; DayCount wasn't being called to calculate the offset, I'd referenced the WRONG form element...
                    var matched++;
    		}
    	}
    I'd also incorrectly coded the offset value from a form element (DayCount) used to index how many days ahead ; an example of how this function might be used is when a shipping company knows it takes X number of BUSINESS days to get the product from Australia to Toronto, including a known trans-pacific surface ship transit time; I haven't including every factor in the example, because it was just the array reference was where I had the problem. To automatically count the exact number of elements of the array, I've revised it to get the value with DateArray.length-1 because I don't plan on confining the tool to just the dates for 2017, but the example I posted was supposed to allow for useful experimenting. So no, 255 won't be the exact number, I was just making sure it went over the entire array. It's NOT whether WE observe Good Friday, but other businesses involved MIGHT, so we can't count on things to move further along during that day.

    But I've got it sorted now, thanks for everyone's efforts to help.

    -SC45

  8. #7
    New to the CF scene
    Join Date
    Aug 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Re: Why I loop...

    If I read the javascript manual right, indexOf is based on pattern matching/regex; if that is so, it will fail all kinds of ways when I input the start date as anything like January or February something, because 12718=~/112718/ and is NOT an EXACT match, but is TRUE; so I use the loop and test for EQUIVALENCE instead of a "match". Similarly, 21718=~/121718/...

    I know Julian Days would resolve that issue, but most ordinary folks don't see the world THAT way...

    -SC45

  9. #8
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,505
    Thanks
    4
    Thanked 503 Times in 491 Posts
    Quote Originally Posted by SandCastle45 View Post
    If I read the javascript manual right, indexOf is based on pattern matching/regex; if that is so
    It's not... it's a literal ===

    Also not sure what "manual" you are referring to, but that's not how it works at ALL. It can't even ACCEPT a regex so... Well, unless you're working in Flash's userscript instead of ACTUAL JavaScript. Then things are utterly and totally banjaxed.

    You're storing as numeric, there should be no problem so long as your date field is returning same... NOT that I'd expect it to since again <input type="date"> doesn't do that, nor would the type="text" fallback in older browsers, meaning unless you're explicity telling people to type it in with no leading zeros, no slashes, in [M]MDDYY, that's not gonna fly.

    In any case, see the MDN page about it:
    https://developer.mozilla.org/en-US/.../Array/indexOf

    Even if you did polyfill loop, there is no reason to keep looping once you have your match, you know how many it is going to add, so just add it and see if you are still in range.

    NOT that I entirely track why you're doing it with the massive array instead of just using proper date handling... well, apart from the skipping weekends part, that could be tricky.
    Last edited by deathshadow; Aug 15th, 2017 at 11:36 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

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