1

I've tried fiddling around with the code, this is the closest I've gotten to accomplishing what I want. As of right now it rewrites the index[0], leaves index[1] untouched, and adds index [2]. I'm trying to get it so it leaves both index[0] and index[1] untouched, just adding the new index.

// Store all accounts and information
var account = [
{
    username: "John Sant",
    password: "dog123",
    balance: 450
},
{
    username: "Rebecca Dunson",
    password: "Munco38",
    balance: 1276
}
]

// Create new user or proceed to sign in
var task = prompt("Do you have an account? (Yes or No)")
if(task.toLowerCase() === "no"){
    for(i = 0; i <= account.length; i++){
        var newUsername = prompt("Enter your first and last name:")
        account[i++] = {username: newUsername}
};
} 

Just focusing on the username at the moment

Sloan
  • 29
  • 2
  • Possible duplicate of [How to append something to an array?](https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array) – Todd Sewell Jun 23 '18 at 23:25

1 Answers1

2

You don't need the for loop. You can just push() a new value to the accounts array to make a new entry.

// Store all accounts and information
var account = [{
    username: "John Sant",
    password: "dog123",
    balance: 450
  },
  {
    username: "Rebecca Dunson",
    password: "Munco38",
    balance: 1276
  }
]

// Create new user or proceed to sign in
var task = prompt("Do you have an account? (Yes or No)")
if (task.toLowerCase() === "no") {
  var newUsername = prompt("Enter your first and last name:")
  var newAccount = {username: newUsername}
  newAccount.password = prompt("Enter a new password:")
  account.push(newAccount)
};

// log all accounts
console.log(account)
Mark
  • 90,562
  • 7
  • 108
  • 148
  • If it was just an explanation, I would vote you up :) – Kick Buttowski Jun 23 '18 at 23:15
  • @KickButtowski, I'm not sure I understand. – Mark Jun 23 '18 at 23:17
  • @Mark_M Now how would I be able to target the newest index to add the password? – Sloan Jun 23 '18 at 23:20
  • @sloan I meant if it was no code , I would vote you up :). Sloan comment is a good prove of why I said that. providing the code will not help anyone. – Kick Buttowski Jun 23 '18 at 23:23
  • @Sloan I would save the object to a variable, prompt the user for a password, add it as a property, *then* push the object to the list. See the edited answer. – Mark Jun 23 '18 at 23:24
  • @Mark_M Thank you very much! It all makes so much sense after seeing/hearing the answer haha – Sloan Jun 23 '18 at 23:30
  • @Sloan I totally respect your opinion – Kick Buttowski Jun 23 '18 at 23:33
  • @KickButtowski Too be fair for my second question regarding the password I didn't even look at the code, I just read his explanation and went from there. In fact, I got it working in a different way then what he wrote. But writing the proper code is helpful (given that you've actually tried to solve the problem prior), once you see it, it just clicks and you're like "Oh that makes total sense, I'll remember that next time" – Sloan Jun 23 '18 at 23:50