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


#1

Hello everyone!

I think there might be a slight bug when trying to do the instructions.

No matter how many times I reset the exercise I always end up with NaN.

‘See screenshot.’

This is ofcourse following the instructions to the T!

Thank you for your time!

```

Replace this line with your code.

<do not remove the three backticks above>

#2

But when getting the code from the site, it ends up looking like this ,

‘See screenshot.’

Which doesn’t make sense as the instructions clearly tells us that : ’ Under the getSubTotal() function, create a function expression using the variable const named getTax. It should take one parameter, orderCount.’.


#3

Hi there, I was confused too about the instruction and end up with the same problem. I think the idea about this is about the use of global variables (something they did not explain). The variable “orderCount” is a variable that changes when you call the function “takeOrder()”, and that variable is used in all functions. However, when you give an argument to the function, that argument becomes a local variable that is used in the function, and is completely different from the global one. They may have the same name, but each one is independent from each other. In your code, do “console.log” to print the argument in the “getTax()” function, it will print “undefined”. This is the reason the result it gives is NaN. When you add a variable with an undefined variable, it gives NaN.

I highly recommend you print the values you are using, so you can understand what is happening. I also encourage to learn about global and local variable, which is SUPER IMPORTANT in programming.


#4

I 'm really starting to think I’m simply too stupid for all of this. I ran into the exact same issue, and despite the correct code being shown, and the explanation by alemto715, I have no clue what’s going on.
25% into this course and still don’t understand a single thing.


#6

Hi @datcrackadan,

Coding needs precise attention to details because we’re telling the computer how to work through the instructions (the code). We can’t expect the computer to guess/autocorrect thing such as console.log("Appl");, when we want it to spell Apple. Basically, you must be very sure of the instructions given (no room for mistakes).

So, sometimes it’s a spelling, sometimes it’s a extra semicolon, sometimes it’s the wrong order/placement of which code executes first etc, any tiny mistakes will throw the code working opposite how we wanted it to do at the first place.

For that, no, you’re not alone in this, don’t feel demotivated, keep it going, when facing issues like the code not working that way you wanted, you have to go through it line by line, that’s debugging (figure out what is making your code faulty).

It’s tiring and tedious but that’s the only way to go, same process even for the pros.


Suggestion: You could’ve posted a topic on your own, provide a link, provide your code, telling us your situation/problem in details so others can help.

(Because a lot of time, users think they have the same problem, it turn out in the end, they’re all different problems/different spelling mistakes/different syntax(extra quotation marks/semicolon) so on)


Since you mentioned you had the same problem like @amsz94,

I’m gonna answer this all together for you both.

This is like finding what’s different in a photo game

@amsz94 could just post the code in here with proper code format so that others can look over the code differences before and after more easily, putting screenshots without links does not make the job any easier. (If it did, others would have answered it already)

If you guys notice, there’s a slight difference in the code before and after the get code in the screenshot @amsz94 posted.

The difference:
Before:

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

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

The code after (Get Code):

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

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

That is what making the code printed NaN at the first place (the before code).

Because initially you declared a function getTax(orderCount) to take in a parameter, then in function getTotal(), you’re calling function getTax() without any argument.


Essentially, you are having the same problem like the other user here.
For more elaboration, you guys can refer my answer there.


Long post, I’ll leave you guys in peace.

Happy coding. Cheers! :slight_smile:


#7

I encountered the NaN result too. I did some hindsight experiments. Based on what codexthon mentioned, removing ‘orderCount’ as a parameter from Instruction 1 will provide you with the answer for part 4.

I’m still quite unsure why the course / algorithm written wanted us to pass that parameter, since my assumption is that we are calling the getSubTotal function which already have the requirement to capture the ‘orderCount’ parameter. Because the course forces us to include it to clear part 1, at that point i thought i was wrong.

Anyone would help to clarify if my assumption (on para 2) is wrong?


#8

Thank you all for the help! I’m still trying to understand the code. I’ve even seen that the code has been changed in the exercises.


#9

I think there is a bug in their lesson code. If you look at the Hint provided in step #3 they are passing a parameter to the getTax() function which requires no parameter. You cannot complete the step without putting this parameter in and the step #4 fails because it is there. I have submitted a bug report on this very topic.


#10

Hi @geekpatrolmiller, @amsz94, @00exia, @datcrackadan,

I guess my reply had created some confusion and I’ve already submitted a report about the few inconsistencies for this lesson. Give them some time if there’s changes to be made.

:small_red_triangle_down: Whole story cut short is that the Get Code answer you’re getting is following the logic/instruction for an older exercise in:
Learn JavaScript track (which will be retired soon)
versus
this lesson in Introduction to JavaScript track (the latest track)

:small_red_triangle_down:You can click the link above and see there are differences even starting from Instruction 1 whereby:

  • Learn JavaScript track (older):

    Let’s start with calculating the tax. Under the getSubTotal function, declare a function named getTax. It should take no parameters.

  • Introduction to JavaScript track (latest):

    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, @amsz94’s question is actually based on the latest Introduction to JavaScript track, but the answer from the Get Code is actually based on the older one, which is the Learn JavaScript track.

I’ve actually posted my opinion based on what @amsz94 provided, which is the Get Code photo, without the relevant link, I was quite reluctant to search through the lesson by myself as I’ve completed that section for a while.

(Which is why users are always being encouraged to post the link towards the lesson when asking questions, it serves a good reference point.)


:pushpin: I’ve compared and list out the inconsistency in the report:

:small_blue_diamond:Under Instruction 1: (Parameter orderCount is needed)

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.

:small_blue_diamond:Next, under Hint for Instruction 2: (Parameter orderCount not showing up in the hint)
** (If they need to change, they only need to change this Hint)

Since you have a getSubTotal() function written, you can call that function inside the getTax() function’s block. Since getSubTotal() needs an itemCount, make sure to pass it an argument. It may look like this:

const getTax = () => {  //<== (notice this) no parameter inserted here
  return getSubTotal(orderCount) ...
};

:small_blue_diamond:Next, under Hint for Instruction 3: (getTax() function taking in an argument of orderCount which is different from the Get Code answer)

You can utilize the getSubTotal() and getTax() functions in the block of the getTotal() function, since both return their results.

It may look like:

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

As I mentioned:

My explanation still remain the same.

Following this lesson rigidly, if you give getTax(orderCount) a parameter, then when you invoke getTax function, you need to supply an argument as well. Invoking getTax() without any argument will print NaN.

To sum it up, see my comment in the code:

const getTax = (orderCount) => {  //<== Instruction 1, need to add orderCount as parameter
  return getSubTotal(orderCount) * 0.06; //<== Instruction 2, need to add orderCount as the argument
};

const getTotal = () => {
  return getSubTotal(orderCount) + getTax(orderCount); //<== Hint Instruction 3, need to call getTax function with orderCount as argument.
};

By looking at the code @amsz94 posted before Get Code, you’re basically missing the part:

return getSubTotal(orderCount) + getTax(orderCount);

since you only have:

return getSubTotal(orderCount) + getTax();

That’s is what causing the NaN.


:sunny: Conclusion:

Point to remember is that when you create a function that takes in parameter, you need to supply argument when invoking/calling/executing the function.

So that’s it, another long post, report submitted. Just ignore the Get Code answer, since it’s not following the Instruction 1 taking in orderCount as parameter.


:bell: Inconsistency in the lesson - summary (Parts needed to be changed):
cc to mods: @mtf @stetim94 sorry for the tag :dizzy_face::zipper_mouth_face::no_mouth::sweat_smile:

  • if orderCount is needed as parameter in function getTax:

    • Change/amend Get Code answer
    • Change/amend Hint Instruction 2
  • if orderCount is Not needed as parameter in function getTax:

    • Get Code answer remain the same
    • Change/amend Instruction 1
    • Change/amend Hint Instruction 3

Code for Return II
Pizza.js
How is this supposed to look with one parameter orderCount
Functions | Return II
#11

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