www.webdeveloper.com
Results 1 to 5 of 5

Thread: Please help me with my JS homework.

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    10

    Please help me with my JS homework.

    I have to write a program using the following instructions:

    1. Ask the user for their first name
    2. Ask the user for their last name
    3. Ask the user for an opening balance for their bank account
    4. Ask the user to choose an action to perform. This action can be a deposit, a withdrawal, or checking their balance. These choices should be offered to the user in a clear menu-styled prompt message such as “Please enter ‘D’ for Deposit, ‘W’ for withdrawal, or ‘B’ to check your balance”.
    5. If the user chooses to make a deposit:
    o Ask the user the amount of the deposit
    o Check that the amount is more than zero. If it isn’t alert the user of the error and ask them for the amount again (you don’t have to check it a second time, for the purposes of this program we will assume one error message is enough and the user will always get it right the second time).
    o Perform the deposit
    6. If the user chooses to make a withdrawal:
    o Ask the user the amount of the withdrawal
    o Check that the amount is more than zero. If it isn’t alert the user of the error and ask them for the amount again (as with deposits, we’ll assume that they will always get it right the second time.
    o Once you have the correct amount, check that their balance is greater than or equal to the amount of the withdrawal. If it is perform the withdrawal. If it isn’t alert the user of the error and don’t perform the withdrawal.
    7. If the user chooses to check their balance:
    o Tell the user their balance.
    8. If the user enters an invalid choice for the menu option alert them of the error.
    9. For a deposit or withdrawal deduct a 3 cent transaction fee.
    10. For all choices (even an invalid entry) accrue 2.25% interest on the account.
    11. After you have this menu working, copy and paste it exactly as it is another two times so that the program will offer the user to perform a transaction three times.
    12. After the third transaction use document.write() statements to give the user a summary which includes at least the following information:
    o A welcome message with the user’s full name
    o The opening balance


    o A summary of deposits which is “There were no deposits” if there weren’t any deposits,
    otherwise the number of deposits and the average amount of each deposit.
    o A summary of withdrawals which is “There were no withdrawals” if there weren’t any withdrawals, otherwise the number of withdrawals and the average amount of each withdrawal.
    o The closing balance

  2. #2
    Join Date
    Oct 2012
    Posts
    10
    And this is how I wrote the code:
    Code:
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	</head>
    	<body>
    		<script type="text/javascript">
    			//	declare variables
    			var firstName;
    			var lastName;
    			var fullName;
    			var openingBalance; //this is the initial balance
    			var closingBalance; //this is closing balance
    			var userInput; //this will store whatever was the input
    			var depositAmount; //deposit you made
    			var withdrawalAmount; //withdrawal you made
    			var ES = " "; //empty space
    			var BR = "<br />"; //break
    			var tempBalance; //to temporarily store balance amount
    			var d = 0; // amount of deposits
    			var w = 0; // amount of withdrawals
    			var totalDeposits = 0; // to store the actual amount from all the deposits that were made
    			var totalWithdrawals = 0; // to store the actual amount from all the withdrawals that were made
    
    			//declare constants
    
    			var TRANSACTION_FEE = 0.03; //transaction fee 
    			var INTEREST_RATE = 0.0225 //interest rate
    
    
    
    			//ask the user to fill in vital info
    			firstName = prompt("Please enter your first name");
    			lastName = prompt("Please enter your last name");
    			fullName = (firstName + ES + lastName);
    			openingBalance = prompt("Please enter your opening balance");
    			openingBalance = parseFloat(openingBalance);
    
    
    			//Ask the user to choose an action to perform
    			userInput = prompt("Hello," + fullName + "." + ES + "Please enter 'D' for Deposit, 'W' for Withdrawal or 'B' to check your balance");
    			if (userInput !== 'D' && userInput !== 'W' && userInput !== 'B')
    			{
    				alert("You must only select D, W or B. Uppercase only!");
    				openingBalance = ((openingBalance * INTEREST_RATE) + openingBalance);
    			}
    
    
    
    
    
    			//This code if for deposit
    			if (userInput === "D")
    			{
    				d = (d + 1); //to count how many deposits were performed
    				depositAmount = prompt("Enter the amount of your deposit");
    				depositAmount = parseFloat(depositAmount);
    				totalDeposits = (depositAmount + totalDeposits); //this is gonna tally up the total amount for all the deposits that were made.
    				if (depositAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					depositAmount = prompt("Enter the amount of your deposit");
    				}
    				else
    				{
    					tempBalance = (openingBalance + depositAmount);
    					tempBalance = (tempBalance * INTEREST_RATE);
    					tempBalance = (tempBalance + openingBalance);
    					closingBalance = (tempBalance - TRANSACTION_FEE);
    
    
    
    
    
    				}
    			}
    
    			//This code is for withdrawal
    
    			if (userInput === "W")
    			{
    				w = (w + 1); //to count how many withdrawals were performed
    				withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				withdrawalAmount = parseFloat(withdrawalAmount);
    				totalWithdrawals = (withdrawalAmount + totalWithdrawals); //this is gonna tally up the total amount for all the withdrawals that were made.
    				if (withdrawalAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				}
    				else if (openingBalance >= withdrawalAmount)
    				{
    
    
    					tempBalance = (openingBalance - withdrawalAmount);
    					closingBalance = (tempBalance * INTEREST_RATE - TRANSACTION_FEE);
    
    
    
    
    
    				}
    				else
    				{
    					alert("Your balance is less than your withdrawal amount!");
    				}
    
    			}
    			//This code is to check the balance
    
    			if (userInput === "B")
    			{
    				tempBalance = (openingBalance * INTEREST_RATE);
    				closingBalance = (tempBalance + openingBalance);
    			}
    
    
    
    
    
    			//**************************************************************************************************************************
    
    			//This code if for deposit 2
    			if (userInput === "D")
    			{
    				d = (d + 1); //to count how many deposits were performed
    				depositAmount = prompt("Enter the amount of your deposit");
    				depositAmount = parseFloat(depositAmount);
    				totalDeposits = (depositAmount + totalDeposits); //this is gonna tally up the total amount for all the deposits that were made.
    				if (depositAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					depositAmount = prompt("Enter the amount of your deposit");
    				}
    				else
    				{
    					tempBalance = (openingBalance + depositAmount);
    
    					tempBalance = (tempBalance * INTEREST_RATE);
    					tempBalance = (tempBalance + openingBalance);
    					closingBalance = (tempBalance - TRANSACTION_FEE);
    
    
    
    
    
    				}
    			}
    
    			//This code is for withdrawal 2
    
    			if (userInput === "W")
    			{
    				w = (w + 1); //to count how many withdrawals were performed
    				withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				withdrawalAmount = parseFloat(withdrawalAmount);
    				totalWithdrawals = (withdrawalAmount + totalWithdrawals); //this is gonna tally up the total amount for all the withdrawals that were made.
    				if (withdrawalAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				}
    				else if (openingBalance >= withdrawalAmount)
    				{
    
    
    					tempBalance = (openingBalance - withdrawalAmount);
    					closingBalance = (tempBalance * INTEREST_RATE - TRANSACTION_FEE);
    
    
    
    
    
    				}
    				else
    				{
    					alert("Your balance is less than your withdrawal amount!");
    				}
    
    			}
    			//This code is to check the balance
    
    			if (userInput === "B")
    			{
    				tempBalance = (openingBalance * INTEREST_RATE);
    				closingBalance = (tempBalance + openingBalance);
    			}
    
    
    
    			//**************************************************************************************************************************
    
    			//This code if for deposit 3
    			if (userInput === "D")
    			{
    				d = (d + 1); //to count how many deposits were performed
    				depositAmount = prompt("Enter the amount of your deposit");
    				depositAmount = parseFloat(depositAmount);
    				totalDeposits = (depositAmount + totalDeposits); //this is gonna tally up the total amount for all the deposits that were made.
    				if (depositAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					depositAmount = prompt("Enter the amount of your deposit");
    				}
    				else
    				{
    					tempBalance = (openingBalance + depositAmount);
    
    					tempBalance = (tempBalance * INTEREST_RATE);
    					tempBalance = (tempBalance + openingBalance);
    					closingBalance = (tempBalance - TRANSACTION_FEE);
    
    
    
    
    
    				}
    			}
    
    			//This code is for withdrawal 3
    
    			if (userInput === "W")
    			{
    				w = (w + 1); //to count how many withdrawals were performed
    				withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				withdrawalAmount = parseFloat(withdrawalAmount);
    				totalWithdrawals = (withdrawalAmount + totalWithdrawals); //this is gonna tally up the total amount for all the withdrawals that were made.
    				if (withdrawalAmount < 0)
    				{
    					alert("Amount less than zero! Enter amount again!");
    					withdrawalAmount = prompt("Enter the amount of your withdrawal");
    				}
    				else if (openingBalance >= withdrawalAmount)
    				{
    
    
    					tempBalance = (openingBalance - withdrawalAmount);
    					closingBalance = (tempBalance * INTEREST_RATE - TRANSACTION_FEE);
    
    
    
    
    
    				}
    				else
    				{
    					alert("Your balance is less than your withdrawal amount!");
    				}
    
    			}
    			//This code is to check the balance
    
    			if (userInput === "B")
    			{
    				tempBalance = (openingBalance * INTEREST_RATE);
    				closingBalance = (tempBalance + openingBalance);
    			}
    
    
    
    
    
    
    
    			//end the program
    
    
    
    			document.write("Welcome," + ES + fullName + "!" + BR);
    			document.write("Your opening balance is" + ES + openingBalance + BR);
    
    			if (d === 0)
    			{
    				document.write("There were no deposits." + BR);
    			}
    
    			if (w === 0)
    			{
    				document.write("There were no withdrawals." + BR);
    			}
    
    
    			if (d !== 0)
    			{
    				totalDeposits = (totalDeposits / d);
    
    				totalDeposits = parseFloat(totalDeposits);
    				document.write("The number of your deposits was" + ES + d + BR);
    				document.write("Your average deposit was" + ES + totalDeposits + BR);
    			}
    
    
    			if (w !== 0)
    			{
    				totalWithdrawals = (totalWithdrawals / w);
    				totalWithdrawals = parseFloat(totalWithdrawals);
    				document.write("The number of your withdrawals was" + ES + w + BR);
    				document.write("Your average withdrawal was" + ES + totalWithdrawals + BR);
    			}
    
    
    
    
    
    
    
    
    
    			document.write("Your closing balance is" + ES + closingBalance + BR);
    			document.write("Good-Bye, " + fullName + "!" + BR);
    
    
    
    
    
    
    
    
    
    
    
    
    
    			
    		</script>
    	</body>
    
    </html>

    My teacher told me that I am not using if/else properly and that the math in my deposit and withdrawal sections is wrong. I cannot understand what I did wrong and why. Can someone help me please?

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    Your teacher is right ! There is no need to write
    Code:
    if (d === 0) {//...
    			}
    if (d !== 0) {//...
    			}
    // Better is 
    if (d === 0) {//...
    			}
    else {//...
    			}
    // Same thing for w
    and above all withdrawals must be ever more difficult to make that deposits!
    Last edited by 007Julien; 10-23-2012 at 12:38 PM.

  4. #4
    Join Date
    Oct 2012
    Posts
    10
    Quote Originally Posted by 007Julien View Post
    Your teacher is right ! There is no need to write
    Code:
    if (d === 0) {//...
    			}
    if (d !== 0) {//...
    			}
    // Better is 
    if (d === 0) {//...
    			}
    else {//...
    			}
    // Same thing for w
    and above all withdrawals must be ever more difficult to make that deposits!
    but w cannot be if/else because I also have to account for wrong input...

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    There is also an if (w===0) and few lines later an if (w!==0)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles