Code for Return II


#1

This is the code by which you can get onto next chapter from Return II… but it doesn’t work on other IDEs

var orderCount = 0;

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

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

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

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

takeOrder('mushrooms', 'thin crust');
takeOrder('pineapple', 'mozarella-filled crust');
takeOrder('chilli', 'no crust');

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

This is the real code which works but not on Codecademy…

var orderCount = 0;

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

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

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

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

takeOrder('mushrooms', 'thin crust');
takeOrder('pineapple', 'mozarella-filled crust');
takeOrder('chilli', 'no crust');

console.log(`Subtotal is $${getSubTotal(orderCount)}.`);
console.log(`Total sales tax is $${getTax(orderCount)}.`)
console.log(`Total Amount is $${getTotal()}.`);

Return II , followed instructions to the dot, but still having problems
#3

Hi @cssrunner36081,

For that particular exercise in Codecademy, it does not require you to have orderCount as parameter in function getTax

So, your first code:

should be:

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

Change that and it should work on any other IDE.


What happened is that you declared a function getTax which takes in parameter of orderCount, like this:

const getTax = (orderCount) => { //<=== you have a parameter in your function
    return getSubTotal(orderCount) * 0.06;
};

but later in your code, you didn’t call the function getTax with any argument (just empty):

const getTotal = () => {
return getSubTotal(orderCount)+getTax(); //<==== look here, calling the function without any argument
};

Therefore, it’s returning NaN for you.

Observation:
In this case, Codecademy SCT is letting you passed with the wrong code, even when your code printed:

Order: thin crust pizza topped with mushrooms
Order: mozarella-filled crust pizza topped with pineapple
Order: no crust pizza topped with chilli
22.5
NaN

NaN is not the intended outcome of the lesson.

Can’t blame them though except there is more room for improvement, writing a complete SCT which caters for all possibilities is not an easy job.


Another note:

Although your second code works, but having a global variable named orderCount and using it as parameter will eventually lead to more confusion if you are having thousand lines of code in later stage, hence, it’s not recommended to do it this way, it works, but not a good practice.

const getTax = (orderCount) => { //<=== orderCount here is a parameter
    return getSubTotal(orderCount) * 0.06;
};

const getTotal = () => {
    return getSubTotal(orderCount) + getTax(orderCount); //<=== orderCount here is an argument which uses the global variable declared initially
};

//both are two different things

Hope this clarifies. :slight_smile:


Return II , followed instructions to the dot, but still having problems
#4

Thanks @codexthon :slight_smile:


#5

Personally i dony understand this, the lessons says

“Let’s start with calculating the tax. Under the getSubTotal() function, create a function expression using the variable const named getTax. It should take one parameter, orderCount.”

When i read this i get:

Blockquote
const getTax = (orderCount) => {
}

Then it tells us to
"Let’s start with calculating the tax. Under the getSubTotal() function, create a function expression using the variable const named getTax. It should take one parameter, orderCount."

SO when i see this i add

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

Where is my error in thinking as im folllowing the directions/?


#6

Hi @codewhiz81664,

I’ve posted a long reply here. Go through it if you have time. Cheers :slight_smile:


#7

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