0

Thanks to @Aplet123 I can store literal strings in an array as functions. (previous issue) This works great. But I can’t figure out how to do the same with nested arrays. For example:

var nameTemp = `Placeholder`;
Title[0] = () => `what are you called?`;
Title[1] = () => `what does ${nameTemp} do?`;
Title[2] = () => `How old is ${nameTemp} ?`;

//--I get the users name from an input.text element and store in nameTemp
textbox.textContent = Title[1]();

This works well. It returns “what does [name entered] do?” and updates the Literal string when Title[x]() is called.

But I have multiple text items per page and I’d like to be able to retrieve them using something like:

Title[1][1] = () => `what does ${nameTemp} do?`;

I’ve tried various things, but they either don’t return the length of the second array, or don’t run the function:

var testC = [];
testC[0] = [];
testC[0][0]  = ()  => ` what does ${nameTemp} do?`;
console.log(testC[0][0]);  

this just returns “() => ` what does ${nameTemp} do?” it doesn’t run the function.

Any ideas? Thanks in advance for any help.

1 Answers1

0

It returns the function and you forgot to call the function using ()

var nameTemp = 'John';
var testC = [];
testC[0] = [];
testC[0][0]  = ()  => ` what does ${nameTemp} do?`;
console.log(testC[0][0]()); 
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
  • Unbelievable – that deserves the biggest face slap! I can’t believe I missed that. I’ve been staring at this for hours – talk about not seeing the wood for the tress. Thank you very much!! That’s made my life much easier!! – user13057690 Apr 30 '20 at 10:08