3

So I'm basically making an savings account system. A user can set up an account and start with a balance of 0 the withdraw and deposit money whenever they want. It works perfectly the first few times but then it gives me the RangeError either when I try logging in or out.

setScreen('LogIn');
      var balance;
      var curbal = [];
      var totalAccts=[];
      onEvent("saveAcct", "click", function() {
        var username = getText('createUserInput');
        var password = getText('createPassInput');
        appendItem(totalAccts, getText('createUserInput') + " " + 
getText('createPassInput') + " ");
        console.log("Username: " + username + " Password: " + password);
        console.log(totalAccts);
        setScreen('LogIn');
      });

      onEvent('makeAcct', 'click', function(){
        setScreen('CreateAcct');
        setText('createUserInput', "");
        setText('createPassInput', "");
        setText("userInput", "");
        setText("passInput", "");
        hideElement('alert');
      });

      onEvent('addMoney', 'click', function(){
        setNumber('balance', balance + getNumber('addInput'));
        balance = balance + getNumber('addInput');
        setNumber('addInput', '');
        console.log(balance);
      });

      onEvent('subMoney', 'click', function(){
        setNumber('balance', balance - getNumber('subInput'));
        balance = balance - getNumber('subInput');
        setNumber('subInput', '');
        console.log(balance);
      });

      onEvent('login', 'click', function(){
        balance = 0;
        changeEnBal(totalAccts, curbal);
      });

      onEvent('logout', 'click', function(){
        changeExBal(totalAccts, getNumber('balance'), curbal);
        hideElement('alert');
        setText('balance', 0);
        setText("userInput", "");
        setText("passInput", "");
        setScreen('LogIn');
      });

      onEvent("goBack", "click", function() {
        setScreen('LogIn');
      });

      function changeEnBal(list, balList) {
        var user = getText('userInput');
        var pass = getText('passInput');
        for (var i = 0; i < list.length; i++) {
            if (list[i] === (user + " " + pass + " ")) {
              setScreen('Account');
            } else {
              for (var a = 0; a < balList.length; a++) {
              if (list[i] === (user + " " + pass + " " + balList[a])) {
                balance = balList[a];
                setNumber('balance', balList[a]);
                setScreen('Account');
                console.log(balList[a]);
              }
            } 
          }
        }

        for (var b = 0; b < balList.length; b++) {
          if (balList[b] != (user + " " + pass + " " + balList[b])){
            showElement('alert');
          }
        }
      }
      function changeExBal(list, curBal, balList) {
        var user = getText('userInput');
        var pass = getText('passInput');
        for (var i = 0; i < list.length; i++) {
            if (list[i] === (user + " " + pass + " ")) {
              list[i] = user + " " + pass + " " + curBal;
              appendItem(curbal, curBal);
              console.log(curbal);
              console.log(list[i]);
            } else {
                for (var a = 0; a < balList.length; a++) {
                if (list[i] === (user + " " + pass + " " + balList[a])) {
                  balList[a] = curbal;
                  list[i] = user + " " + pass + " " + curBal;
                }
              }
            } 
        }
        console.log(totalAccts);
      }
4
  • Why are you initialized i to -1? It'll result in undefined since array accession with brackets doesn't understand negative indexes Commented Mar 30, 2017 at 23:37
  • 2
    Arrays start at index 0 not -1 so you might want to change your for loop declarations. Also, we need to see some data that you are sending for list and balList. Commented Mar 30, 2017 at 23:37
  • 1
    Is this an AppLab app? And if so, could you provide a link to the project? Commented Mar 30, 2017 at 23:42
  • studio.code.org/projects/applab/… here is the link to the code and app Commented Mar 31, 2017 at 0:34

1 Answer 1

1

As suggested in some of the comments I would change your for loops to start with a zero index:

for (var i = 0; i < list.length; i++)

and

for (var a = 0; a < balList.length; a++)
Sign up to request clarification or add additional context in comments.

1 Comment

I changed the index of the for loops as you guys said.

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.