1

I am trying to make it so that when I run the function it checks if there is a save already. If there isn't, then it puts those values in the list. I am using nested arrays and there will be three total saves. But for some reason when I run it, it says that allSaves[L] is undefined. But when I put a zero in for the L, it works. But I can't do that. So does anyone know how I could fix that? Here is my code -


    function savingList(principal, interestRate, time, compoundNumber) {
       var allSaves = new Array();
      allSaves[0] = new Array();
      
      for(var L = 0; L < 2; L++) {
        if(allSaves[L].length == 0){
          allSaves[L] = new Array(principal, interestRate, time, compoundNumber);
        }
      }
    } 

2
  • I guess the problem happens when your loop hits L=1. Commented Jan 8, 2021 at 3:46
  • oh wow. I forgot to put break. I feel stupid now but its fine. Thank you! Commented Jan 8, 2021 at 3:50

1 Answer 1

2

I would suggest creating allSaves directly as an Array.map() instead of using a traditional loop block. Also note that your iterator variable L should be a let instead of a var so that it is scoped appropriately.

function savingList(principal, interestRate, time, compoundNumber) {
  return [...new Array(3)].map(saveIteration =>
    [principal, interestRate, time, compoundNumber]
  )

The phrase [...new Array(i)] is just a way of getting a map-able array of length i.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.