I'm working through a course on JavaScript currently and honestly I'm having a pretty difficult time with it. If anyone has any resources they could recommend for learning, I'd appreciate it!
I'm trying to make a simple banking app with these requirements:
Here's my basic HTML for a very simple site
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Welcome!</h1>
<button onclick="start()">Click here to get started.</button>
<script src="scripts.js"></script>
</body>
</html>
And here's as far as I've been able to get with the JS...
function start() {
let input = prompt('What would you like to do?');
if input = 'w' {
alert('Withdraw');
} else () {
}
}
And I know that's probably not even the direction I need to be going. I'm honestly stuck because I don't really know where to start. Any and all advice is much appreciated, TIA!!
Regarding resources, you can basically use any recorded or written tutorial. I also like code challenges. To learn while you code, go step by step and use Google :) As you may have done for "get user input javascript".
You have already collected some user input. Try to reuse this for the other things a user can do. Run calculations based on the value the user enters. Finally, record and store the balance.
At a later point, when you learned the new concepts, you can even try persisting the balances using a database, cookie, or web storage.
In the following example (thanks to ruleboy21) the user can enter actions in any order. There is no check yet if there's enough money left, etc.
Play around with this code to learn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Banking app</h1>
<p>Click the button below to start the app. Options: (w)ithdraw, (d)eposit, (b)alance, (q)uit</p>
<button onclick="start()">Click here to get started.</button>
<script type="text/javascript">
let balance = 0;
function start() {
let input = prompt('What would you like to do?');
// debug output for testing
// Open the inspector to see user input
// console.log(input);
if (input == 'q' || input == "") { // Enter quits as well.
return;
} else if (input == 'w') {
withdraw(); // see functions below
} else if (input == 'd') {
deposit();
} else if (input == 'b') {
showBalance();
} else {
alert('Unsupported action: use (w)ithdraw, (d)eposit, or (b)alance. Press q or enter to quit');
}
start(); // restarts the app after action
}
function deposit() {
let amount = prompt('Enter an amount to deposit');
amount = isNaN(amount) ? 0 : parseFloat(amount);
balance += amount;
return balance;
}
function showBalance() {
alert('Current balance: '+balance);
}
function withdraw() {
let amount = prompt('Enter an amount to withdraw');
amount = isNaN(amount) ? 0 : parseFloat(amount);
balance = balance - amount;
return balance;
}
</script>
</body>
</html>