Other than the syntactical problems Wyatt pointed out there was another issue. You do not need for-loop to iterate over the entries in the bankAccountsList object. Objects in Javascript have name-value pairs, where the name is used as the key to access the value and it needs to be unique.
Therefore you can simply look an account up by doing the following: backAccountsList[accountNumber]. This is much a faster (O(1) time complexity) than iterating through all the possible entries in the object.
In addition to the change above I also cleaned up the code by removing all the unused variables and fixed the missing braces in the code.
let bankAccountsList = {
126347869: 200,
234539790: 400,
348079070: 900
};
function withdrawMoney(amount, accountNumber) {
if (accountNumber.length == 9) {
let currentAmount = bankAccountsList[Number(accountNumber)];
if (currentAmount != undefined) {
let message = "<br/>" + "Account: " + accountNumber + "<br/>";
message += "Money in the account: $" + currentAmount + "<br/>";
if (amount <= 2000 && amount > 0) {
currentAmount = currentAmount - amount;
message += "Amount withdrawn: $" + amount + "<br/>";
message += "Updated account balance $" + currentAmount;
return message;
}
return "Enter the amount between 0 and 2000";
}
}
return "<br/>" + accountNumber + " bank account does not exist" + "<br/>";
}
console.log(withdrawMoney(100, "348079070"));
console.log(withdrawMoney(-10, "126347869"));
console.log(withdrawMoney(2001, "234539790"));
console.log(withdrawMoney(1, "234539790"));
console.log(withdrawMoney(100, "126347869"));
console.log(withdrawMoney(500, "234539790"));
console.log(withdrawMoney(50, "34567790"));
Istvan S.
05/10/20