Returns II (6/7) - Efficiency


#1

Hello all. I’ve done a fair amount of JS dev in the past but I’m by no means a developer so I’m taking the JS class here to try and build a better foundation and deep dive on understanding how and why stuff works. As part of that I’ve noticed that the code you get when you “get answer” on this section seems to be quite redundant. Not a criticism of the teaching or anything just trying to understand at a deeper level.

I’ve added comments inline in the sample code below (Codecademy provided code) as well as my own code block after that. Just trying to understand efficiency here.

Codecademy provided code

var orderCount = 0;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}
//Runs every time, you pass in the itemCount (or orderCount) and it returns the math. Why not store the result in a variable?
function getSubTotal(itemCount) {
  return itemCount * 7.5;
}
//This requires taking the result you just got in the function above but rather has you rerun the function.
function getTax() {
  return getSubTotal(orderCount) * 0.06;
}
//This does the same, yet this time runs 3 functions to get the data that could have been had with one variable right?
function getTotal() {
  return getSubTotal(orderCount) + getTax();
}


takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));

console.log(getTotal());

My understanding

var orderCount = 0;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}
//Set output from function in a variable (not using var because my understanding is that would scope it to use only within the variable itself). That may be incorrect or just not explained yet.
function getSubTotal(itemCount) {
  subTotal = itemCount * 7.5;
  return subTotal;
}
//gets variable from above function and performs new math, stores that in a new variable
function getTax() {
  tax = subTotal * 0.06
  return tax;
}
//adds both previous variables, returns total
function getTotal() {
  total = subTotal + tax;
  return total;
}


takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));
console.log(getTotal());

My question, I tested this and it seems that I’m getting an error of “tax not defined”. Not sure why that is but assuming I’m doing this wrong. (perhaps I need to run the function, store it in a variable somehow after then return the result? Just at a bit of a loss here and any explanation would be awesome. Wanting to know more of the why and how and less of a solution for this specific case.


#2

You’re actually completely correct. Most, if not all of the code done here is redundant in the scope of writing an actual application for taking orders. But the purpose of the lesson is to give users practice with the return return command.

It’d be entirely possible to minimize the amount of code written to a single function, and that would be the “best practice” as it were, but teaching people to write efficient code is significantly harder unless they already have a decent handle on the basics.

This was a really good question though, and I hope I explained it well enough!


#3

Here

you really want to calculate the tax which you then add to the calculated =subTotal=
like
total = getSubTotal() + getTax();


#4

Yes, but if I did that I’m still rerunning getSubTotal and getTax. So I’m not saving anything at that point, the code is just slightly smaller.


#5

I think I’ve found the best way to do this after testing my code a bit. Perhaps this can be improved on even more, but seems to be the most clear way to do this.

var orderCount = 0;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}
//Set output from function in a variable (not using var because my understanding is that would scope it to use only within the variable itself). That may be incorrect or just not explained yet.
function getSubTotal(itemCount) {
  subTotal = itemCount * 7.5;
  return subTotal;
}
//gets variable from above function and performs new math, stores that in a new variable
function getTax() {
  tax = subTotal * 0.06
  return tax;
}
function getTotal() {
  //Added here! getTax has never been called anywhere where as getSubTotal has been. I needed to run in order to get the data into a variable.
  getTax();
  total = subTotal + tax;
  return total;
}


takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));
console.log(getTotal());```

#6

But i hope you are aware
that declaring a variable without using the var-keyword withon your function-body
like

function getSubTotal(itemCount) {
  subTotal = itemCount * 7.5;
  return subTotal;
}

you are creaitng 4 =global= variables
console.log(total,subTotal,tax,orderCount);


#7

Well itemCount is already a global variable (as it’s defined at the global scope) but I can see how subTotal is also added. Is it generally better to have more global variables or run functions over and over? My thought would have been that keeping in memory is cheaper but that could be incorrect. (I get this can vary just looking for general guidance and why).

Additionally, I think I would expand some of these functions to include an if statement that checks to see if the variable exists before executing the code. That way if/when there are times where you need a variable and it may have not run it can and if it has run it doesn’t dup the result. Hope that’s clear.

Thanks for the feedback!


#8

function getSubTotal(itemCount) {
return itemCount * 7.5;
}

//Why do we add here ‘itemCount’ as a parameter, cuz if I replace anything else or just remove it out, it will still log the same to the console.

//Even if I replace it with orderCount still everything alright. So I don’t really get the point of putting a parameter which is no where else used.


#9

There are two things.

#1 - I believe that an older version of this course must have used itemCount as a variable and it’s still in the code running that you don’t see. That is the only explanation I could figure for why itemCount is working.

#2 - if you don’t use itemCount/orderCount as a parameter in the function then you can’t pass in data (in this case a variable). You could write the function like so:

var itemCount = 0;
…
function getSubTotal() {
  return itemCount * 7.5;
}

getSubTotal();

The “…” would be the function that runs in the if/else to add to the item count for every order. In the section we are discussing the number is 3, but arguable that could change. My guess is they are showing you to pass the var in as a parameter because of good standards, and not necessarily because it’s needed here. Someone with more knowledge than me can correct me if I’m wrong and likely has a better explanation.


#10

@objectrunner58719
You might read a few articles found with google search
javascript what best to use global var vs local var site:stackoverflow.com

I’ll leave you in peace…


#11

Awesome. Thanks for the help!


#12

Thank your for reply, but the problem is that there is no any kind of variable with the name itemCount. Here the code of @objectrunner58719

var orderCount = 0;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}
//Runs every time, you pass in the itemCount (or orderCount) and it returns the math. Why not store the result in a variable?
function getSubTotal(itemCount) {
  return itemCount * 7.5;
}
//This requires taking the result you just got in the function above but rather has you rerun the function.
function getTax() {
  return getSubTotal(orderCount) * 0.06;
}
//This does the same, yet this time runs 3 functions to get the data that could have been had with one variable right?
function getTotal() {
  return getSubTotal(orderCount) + getTax();
}


takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));

console.log(getTotal());

#13

You are correct. My point was that I believe itemCount may be defined as a variable somewhere within codecademy’s scripts that you can’t see. Hope that helps.


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.