Functions


#1

My exercise here.

The comment part is in grey, also the original code. I recode it and it displays NaN, don’t know why. Can anyone help?


#2

Difficult to say without the code, please copy paste your code to the forum so we can have a look


#3

Here mine! I don’t know why we have to add itemCount, why not insert directly orderCount into function getSubTotal?

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());
*/
const getSubTotal=(orderCount)=>{
  return orderCount *7.5;
}
const getTax=()=>{
  return getSubTotal()*0.06;
}
const getTotal=()=>{
  return getSubTotal()+getTax;
}
console.log(getSubTotal());
console.log(getTotal());

#4

this is possible, but as explained here:

https://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice

it makes your code more prone to bugs. This might be difficult to understand at first (when you start programming)

personally, i would have designed this program differently.

for example:

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

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

const main = () => {
    let orderCount = 0;
    orderCount = takeOrder(orderCount, 'mushroom', 'thin crust');
    orderCount = takeOrder(orderCount, 'spinach', 'whole wheat');
    orderCount = takeOrder(orderCount, 'pepperoni', 'brooklyn style');
    subTotal = getSubTotal(orderCount);
}

#5

Can you explain the flow of main function? I see that for the:

1st time, we pass orderCount, which is 0, into takeOrder function and orderNumber receive value 0, it runs, orderNumber ++, orderNumber receive value 1, you return orderNumber.

2nd time: You pass again the value 0 of orderCount into orderNumber, and the process loop, the orderNumber toggle between 0&1.

How can it reach 3?


#6

i do not:

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

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

const main = () => {
    let orderCount = 0;
    orderCount = takeOrder(orderCount, 'mushroom', 'thin crust');
    console.log(orderCount);
    orderCount = takeOrder(orderCount, 'spinach', 'whole wheat');
    orderCount = takeOrder(orderCount, 'pepperoni', 'brooklyn style');
    subTotal = getSubTotal(orderCount);
}

the returned value is stored in orderCount, increasing the orderCount variable within the main function.


#7

So when you return value of orderNumber, it’s stored in takeOrder?
And when you return value of orderCount*7.5, it’s stored in getSubTotal?


#8

return is literally what is say, returning (handing something back) which we can then store in a variable (we capture the returned result in a variable)

this design is better because we can test the individual function (takeOrder, getSubTotal and so on) with unit tests, and integration test for the main function to test the entire programming is working properly.

The program design the exercise suggest is more difficult to test (due to the global variable), and there is no real way to test where it all comes together, for an integration test

I disagree personally with the design the exercise picks for this program.

So yes, you can manipulate global variable, but i would advise against it. This might seem strange and difficult at first, and that is okay


#9

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