Why no Variables?


#1

Hi guys, this is the answer code from Function >> 6 Return II. It’s about pizza topping and fees.
https://www.codecademy.com/courses/learn-javascript-functions/lessons/functions/exercises/return-ii?action=lesson_resume&program_content_id=7ab3b34edca64ceb6005f8e1e4410d30&program_id=d8a267a92c1ab35ac170bcfafc17800f
:

Two questions:
1: Why do we have the new variable of orderCount from itemCount. I thought they are interchangeable. (Italianized below)
2: Why some functions can have no variables(I bolded them below)

CODES:

let orderCount = 0;

const takeOrder = (topping, crustType) => {
  orderCount++;
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

const getSubTotal =         (itemCount)      => {
  return itemCount * 7.5;
};

const getTax = () => {
 return getSubTotal       (orderCount)       * 0.06; 
};

const getTotal = () => {
  return getSubTotal (orderCount) + getTax(); 
}; 

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

Would appreciate if you could help.


#2

please read this stackoverflow question:

using global is a bad idea, and you update a global variable within a function, which does not strike me as a good design. This is why itemCount is used, manipulating a global variable directly within several functions is more prone to bugs

Now, for a program as small as you have, its fine. But its not best practice.

they are named parameters and arguments, again, its a design choice. Its up to the programmer to decide what parameters are needed and when no are needed parameters at all


#3

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