x-posted from stack overflow

From: CHYRON (DSMITHHFX)30 Oct 2018 15:23
To: ALL1 of 12
(thought I'd run it past teh while I'm at it spinning my tires for the past 3-hours  :-( )

I'm trying to add items from a modified list of thumbs to a service worker storage cache by JSON import.

Everything's working as far as generating a correctly-formatted and complete array of thumbs to cache -- console.log(cacheArray) inside the for statement -- but I can't seem to get a final array out of the function -- console.log('updateCache: ' + cacheArray) returns a blank:

    fetch('alldates.txt').then(function(response) {
        return response.text();
    }).then(function(string) {
        var alldatesobj = JSON.parse(string);
        var cacheThumb;
        var thumbslist = Object.values(alldatesobj);
        var cacheArray = new Array();
        function updateCache(cacheArray) {
            for(i = 1; i <= thumbslist.length; i++) {
                cacheThumb = thumbslist[i].replace('100315', '100315/thumbs');
                //^^^ array is correctly formed!!!
            return cacheArray;
        updateCache(console.log('updateCache: ' + cacheArray));
EDITED: 30 Oct 2018 15:35 by DSMITHHFX
From: Chris (CHRISSS)30 Oct 2018 20:25
Should cacheArray be populated with an array after the updateCache function?

I've not done much JS but I think you want to pass the empty array to updateCache to populate it, then print it out? updateCacheArray expected the cacheArray array, but you're passing the console.log to it. So instead of updateCache(console....) you'd do
From: Chris (CHRISSS)30 Oct 2018 20:27
To: Chris (CHRISSS) 3 of 12
Or, if this works:
console.log('updateCache: ' + updateCache(cacheArray));
From: CHYRON (DSMITHHFX)30 Oct 2018 20:51
To: Chris (CHRISSS) 4 of 12
Nope, function either fails silently, or passes the original, empty array variable.

Weird thing is it logs the array build perfectly all the way to completion from inside the "for" statement, just can't get at the built array outside.
EDITED: 30 Oct 2018 20:55 by DSMITHHFX
From: Chris (CHRISSS)30 Oct 2018 20:55
cacheArray = updateCacheArray(cacheArray); ?
From: Peter (BOUGHTONP)31 Oct 2018 00:03

You should pick either passing in all the variables as arguments, or fully using closure - doing a mix of both is confusing, and is going to be the reason for your bug - you have two different cacheArray variables, and you're not assigning the inner one to the outer one.

What does alldates.txt contain?

If it's already an array then you can just do:

var cacheArray = JSON.parse(string).map
		return item.replace(/100315/,'$&/thumbs');

If the Object.values is needed you can still use map:

var cacheArray = Object.values(alldatesobj).map
		return item.replace(/100315/,'$&/thumbs';

The $& means whatever is matched (pretty much everyone else uses $0 or \0 but JS has to be awkward).

Also, on a general note, you can't trust console.log in modern browsers - they do not tell you a variable's value at the point when you're logging it but have started looking it up after - you need to use console.log(JSON.stringify(variabletolog)) to freeze it and get a correct report.

EDITED: 31 Oct 2018 00:05 by BOUGHTONP
From: CHYRON (DSMITHHFX)31 Oct 2018 13:21
To: Peter (BOUGHTONP) 7 of 12
var cacheArray = Object.values(alldatesobj).map
		return item.replace(/100315/,'$&/thumbs';
Yeah that works! (bounce)

I had read about the map function, but according to some folks somewhere on the internet, it should not be needed and costs more cpu cycles (or something).
What does alldates.txt contain?

It's an array with key-value pairs, keys are 4-digit dates (mmdd), values are image names (*.jpg) used to display a specific new image each day here:
http://designartcraft.com/photo/afbp.htm and randomly here:

The purpose of all this is to cache thumbs (same names, in "/thumbs") for offline.

I'm slowly trying to get up to speed on modren javascript syntax without relying on jquery (which doesn't work in service workers). Long ways to go, obviously.


EDITED: 31 Oct 2018 13:32 by DSMITHHFX
From: Peter (BOUGHTONP) 1 Nov 2018 00:34
> it should not be needed and costs more cpu cycles (or something).

The same can be said for anything that isn't assembly language.

There's no situation where map is necessary - it can always be replaced with a loop - but there's plenty of situations when it makes code easier to understand and work with, and that should generally be the primary concern for the first iteration of any code. (Which isn't to say always use map - it's possible to misuse just like any construct - but don't be afraid to use it when suitable.)

Performance matters when you can show the difference - if a web script takes half a second to run, then absolutely spend some time to identify and optimise the slowest aspect (and that's not always the most obvious one), but if its 38ms vs 35ms then it's less likely to matter.

> I'm slowly trying to get up to speed on modren javascript syntax without relying on jquery

I recently decided to write a jquery-free library for scattering images, and it's interesting to see what's now possible and how browser vendors have still managed to over-complicate simple tasks.

I found MDN to be the best resource, although sometimes you need to know what to look for first - definitely useful for getting the right polyfills if you want to support older browsers whilst still using modern methods.

From: CHYRON (DSMITHHFX) 1 Nov 2018 10:18
To: Peter (BOUGHTONP) 9 of 12
I'm using MDN quite a lot. Explanations are sparse, useful examples rare, and there's an assumption of underlying familiarity with syntax that can make it pretty difficult to follow.
From: Chris (CHRISSS) 1 Nov 2018 21:15
To: Peter (BOUGHTONP) 10 of 12
What is polyfill?
From: Peter (BOUGHTONP) 1 Nov 2018 22:39
To: Chris (CHRISSS) 11 of 12
It's the thick paste you use on walls to smooth over cracks.


It's also when you re-implement native functionality from newer browsers in JavaScript for the benefit of older browsers - see for example the Array.prototype.map polyfill.

EDITED: 1 Nov 2018 23:00 by BOUGHTONP
From: Peter (BOUGHTONP) 1 Nov 2018 22:59
> Explanations are sparse

I prefer the term concise - no need to dredge through unnecessary waffle to figure out what something does.

> useful examples rare

Possibly. On most sites I tend towards going straight to the examples because the explanations are waffle, but don't need to with MDN.

> there's an assumption of underlying familiarity

That'll be because it's mostly a reference.

There is a JavaScript Guide which appears very comprehensive, but looking at a few pages it does a horrific job of explaining stuff and has some terrible examples, so, yeah... that sucks. :/

EDITED: 1 Nov 2018 23:01 by BOUGHTONP