Phone Sale Exercise in YDKJS

Hello community,

I’m doing the first exercise in the book You Don’t Know JavaScript for those who are familiar. In this exercise basically is asking to find the total purchase of phone, accessories, and taxes compared this to an account balance set by a minimum spending.
You Don’t Know JavaScript

I created a function that calculates the total, but when I make the comparison to an If else statement it always returns in the console log the else part even if the condition is true.

I have the suspicion the problem relies in the scope of var maxSpending in the if else statement but I don’t know how to figure out, or if I’m missing something in the code. Any ideas or suggestion based on my code.

var bankAccountBal = prompt("How much do you have in your bank account?");
var phonePrice = 149.99;
var accessory = 9.99;
var maxSpending = alert("You can spend up to: $" + bankAccountBal/3);
const tax_Rate = 0.0925;

//This function will calculate total of purchase of phone, accessory and tax. Step by step in one whole function. 

var totalAmount = function(phoneQuantity, accQuantity){
  numOfPhonePrice = phonePrice * phoneQuantity;
  numOfAccesories = accessory * accQuantity;
  subtotal = numOfPhonePrice + numOfAccesories;
  total = subtotal + (subtotal* tax_Rate);
  console.log("Your total purchase: " + total.toFixed(2));
  return total.toFixed(2);

totalAmount(1, 0);

//Condition to check if affordable taking a threshold expense maximum. I'm setting max spending of bankAccountBal divided by 3.  

if(maxSpending > totalAmount){
  console.log("You can make this purchase.\nGo Ahead!");
} else{
  console.log("This exceeds your spending limit.");

Sorry for the long comments in the snippet.

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


unlike prompt, the return value of alert is not meaningful - it’s just the default undefined of any js function

you want to set maxSpending on its own

const maxSpending=bankAccountBal/3

then use it in the alert

alert("You can spend up to: $" + maxSpending)

@ppc beat me to making a comment about maxSpending. I will instead point out another problem with the same if statement

if(maxSpending > totalAmount){

Above you are asking if maxSpending is greater than a function. What??? totalAmount is a function and not a value. You need to assign the result of calling the function to another variable (let’s call it theTotal) like:

var theTotal = totalAmount(1, 0);

Then, you if statement would be:

if(maxSpending > theTotal){

OR if you don’t need this amount anywhere else in your code your if statement could simply be:

if(maxSpending > totalAmount(1,0){

Thank you both responding.

@rmdawson71 Thank you for fixing the markdown. I didn’t know how to do it.

Regarding the issue of the topic, I thought a function variable also stores values in it that can be just invoked anywhere, that’s why I created a special var “totalAmount” instead of just naming the function like function totalAmount(){…} then calling the function by the variable name.

So by creating a variable that serves to store the result of the function, I’m wondering how practical and flexible it is when in this case I need to change the values of the function phoneQuantity and accQuantity. That means I have to create a new variable for each change of the arguments in the function? At least when calling the function is flexible changing the arguments:

totalAmount(2, 3);
totalAmount(1, 5);//You get the idea

I don’t need to create a new variable for every change in the arguments.

Thanks everyone for your help. I guess I’m not understanding JavaScript very well.

phoneQuantity and accQuantity are local parameter variables for the totalAmount function - they do not exist outside of the function - each invocation/call of the function creates new parameter variables that are assigned the values passed in for the call

totalAmount(2, 3) // new phoneQuantity=2, new accQuantity=3 inside totalAmount
totalAmount(1, 5) // new phoneQuantity=1, new accQuantity=5 inside totalAmount

The totalAmount variable you assigned the function does not store the result of the function - it stores the function itself i.e. a reference to the code representing the function - in fact there is no result to store because the function is only defined not called - totalAmount can then be used just like a named function i.e. as if function totalAmount() {...} had been declared

I don’t follow what new variable you mean below

Thank you. In other words var name = function (){…} === funtion name () {…} . If that’s the case, that helps me clarify this.

Well, if we have to create a variable to store the result of the function. Practically we would have to create a new variable each time we change the arguments in the function. For example:

var total1 = totalAmount(1, 1) //if we want to store the results with parameters (1, 1)
var total2 = totalAmount(2, 2) //if we want to store the results with parameters (1, 1)
var total3 = totalAmount(3, 3) //if we want to store the results with parameters (1, 1)

It gets repetitive and it doesn’t serve the purpose of creating of function in the first place. In the case of a function we don’t have to create a new variable just change the parameters. I thought these values were passed each time we call the function.

totalAmount(1, 1);
totalAmount(2, 2 )…and so on;

They’re not exactly the same but it’s a fair start as long as you know there are differences to learn about

was your initial concept of totalAmount that it stores any future result of calling its assigned function? so totalAmount is dynamically updated with every call to its function? Like so

totalAmount(1, 1) // result is 174.78
console.log(`totalAmount ${totalAmount}`) // totalAmount 174.78
totalAmount(2, 2) // result is 349.56
console.log(`totalAmount ${totalAmount}`) // totalAmount 349.56

This cannot work as is since totalAmount cannot both be a function that can be called and the return value of the called function

but it is possible to have a function that retains its last return value - if you’re okay with using different names for the entities like totalAmount for the function and totalAmount.lastvalue for its last return value

Thank you so much +ppc. I need to read more about it but I get frustrated some times. Too many concepts for me to grasp