Can you improve on the Ch 1 practice? Because despite any changes to the constants in the Ch 1 Practice solution, the purchase is always unaffordable. That is it always comes back with "You can't afford this purchase. :("
I tested this by copy/pasting the very text from the digital book into my console. I did it again and again using different values in the constants for the threshold, prices, bank balance, etc... Always You can't afford this purchase. :("
I may look into tweaking the logic for second edition. Thanks.
What about changing it to:
// keep buying phones while you still have money
while (amount + PHONE_PRICE < bank_balance) {
As the loop is intended to buy phones only if there is enough money left.
agree! is always problematic when you try to solve the problem on your own just to find out the "answer" doesn't really work. This is what makes things hard for beginners.
There is also a larger issue in the fact that you are adding the tax after you decide how many phones to purchase. For a more reliable calculation you should add the tax before you calculate how many phones you can afford.
The point of the snippet was to show simple if-then logic, not to have a completely accurate adding program. I understand the points being made here, but really, can we just lighten up? It's not a cash register that was being programmed. The text is for beginner programmers.
_EDIT: I didn't notice the bit about having this re-done in another edition, I'm just gonna leave this here anyway._
Disclaimer: I'm not a _complete_ beginner programmer, but not very skilled either.
I found the description of the pratical task pretty confusing. I got stuck at it for a couple of minutes and then decided to make a similar narrative that made more sense to me (bankBalance
vs. spendingTreshold
).
Then, tinkering with the sample solution and the (expected) inability to achieve the positive outcome was somewhat frustrating to me.
I believe that modifying the practical exercise would lead to more newbie happiness.
for some reason even when i copy and paste your exercise answer i'm receiving errors like Uncaught SyntaxError: Identifier 'ACCESSOR_PRICE' has already been delared. Am I missing something very obvious..
It is because ACCESSOR_PRICE
is a const
, so you can't redeclare it.
it's giving me the error on line 1 though and I wasnt re declaring it anywhere further down
Where are you running it?
in a blank page's console in chrome
Did you run it more than once? That would be why if you did.
That must be why...thought clearing out the console would be enough instead of opening a new instance. Much aprpeciated!
The wording to the problem itself can be better, as I ended up coding this from my understanding of it:
const TAX_RATE = 0.8;
const PHONE_PRICE = 100;
const ACCESSORY_PRICE = 50;
const SPENDING_THRESHOLD = 200;
var bank_balance = 3459;
//Function to calculate tax amount
function calculate_tax(amount){
return amount * TAX_RATE;
}
//Function to make the amount presentable
function format_amount(amount) {
return "$"+amount.toFixed(2);
}
var amount = 0;
var accessory_amount = 0;
//Keep buying phones until you can't any more
while(bank_balance > 0) {
amount += PHONE_PRICE;
//Keep buying accessories as long as you are in the threshold amount
while (accessory_amount < SPENDING_THRESHOLD) {
accessory_amount += ACCESSORY_PRICE;
}
amount += accessory_amount;
//Reduce that from the bank balance
bank_balance -= amount;
accessory_amount = 0;
}
//Calculate tax, format the amount & Print final bill
amount = amount + calculate_tax(amount);
final_amount = format_amount(amount);
console.log("Your Final bill is for:"+final_amount);
Below is my take on the solution for the problem @getify described in Chapter 1. I agree that the directions are confusing, but the exercise is not impossible. Definitely too hard for a Chapter 1 exercise, but a good challenge nonetheless.
Here's a pen of it in action: https://codepen.io/zeckdude/pen/zdOPva
const PHONE_PRICE = 99.99;
const ACCESSORY_PRICE = 9.99;
const SPENDING_THRESHOLD = 200;
const TAX_PERCENTAGE = 8;
var bankAccountBalance = 303.91;
function calculateTax(amount) {
return amount * (TAX_PERCENTAGE/100);
}
function formatAmount(amount) {
return '$' + amount.toFixed(2);
}
function countPurchasedItems(purchasedItems) {
return purchasedItems.phones + purchasedItems.accessories;
}
function calculatePurchaseAmount(bankAccountBalance) {
var subTotal = 0;
var total = 0;
var amount;
var tax;
var purchasedItems = {
phones: 0,
accessories: 0
};
// Is there still enough money in the bank account for at least one phone?
while(bankAccountBalance >= PHONE_PRICE) {
amount = 0;
// Add the phone price to the current loop amount
amount += PHONE_PRICE;
purchasedItems.phones++;
// If the subtotal of all purchases (including tax) isn't over the spending threshold, then add an accessory to the current loop amount
if ((subTotal + calculateTax(subTotal)) <= SPENDING_THRESHOLD) {
amount += ACCESSORY_PRICE;
purchasedItems.accessories++;
}
// If the subtotal for the current item(s) (including tax) isn't over the bank account balance, then add the new items(s) amounts to the subtotal
if ((amount + calculateTax(subTotal)) < bankAccountBalance) {
// Add the determined purchase amount to the running total
subTotal += amount;
// Deduct the current loop amount from the bank account balance
bankAccountBalance -= amount;
}
}
// Add the tax to the subtotal
tax = calculateTax(subTotal);
total = subTotal + tax;
console.log('PURCHASE SUMMARY');
console.log('Total Number of Items Purchased:', countPurchasedItems(purchasedItems));
console.log('Phones Purchased:', purchasedItems.phones);
console.log('Accessories Purchased:', purchasedItems.accessories);
console.log('Subtotal:', formatAmount(subTotal));
console.log('Tax:', formatAmount(tax));
console.log('Total:', formatAmount(total));
}
calculatePurchaseAmount(bankAccountBalance);
I think we should not muddle this program too much, since it is for beginners, only change that should be made is description of task. We should just add what @getify has said "It's not a cash register that is being programmed" and note that the result should always be the same.
Closing since "Up & Going" was replaced with the new "Get Started" book for second edition, so this issue is no longer applicable.
Most helpful comment
The point of the snippet was to show simple if-then logic, not to have a completely accurate adding program. I understand the points being made here, but really, can we just lighten up? It's not a cash register that was being programmed. The text is for beginner programmers.