[SOLVED] Intro to Functions - the result I'm getting is wrong


#1

Hi everyone,
I'm stuck with this exercise. I repeated the section twice but I still can't get it right.

https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/return

No error messages. But the result is wrong. It gives a result of 15, because it's doing 2 * 7.5 whereas it should do 3 * 7.5 = 22.5. It's taking itemCount but it should take orderCount. Not sure how to tell it to do that.

var orderCount =0;
function takeOrder(topping, crustType) {
  console.log('Order:' + crustType + ' pizza topped with ' + topping);
 orderCount = orderCount+1;
}
takeOrder('bacon', 'thin crust');
takeOrder('ham and cheese and olives', 'medium crust');
function getSubTotal (itemCount) {
  return itemCount * 7.5;  
}

console.log(getSubTotal(2));

Thank you for your help


#2

First of all, move your function calls (takeOrder()) to the bottom of the file (but above the console.log). In my opinion, it's easier to read and to maintain.

First of all, you're passing the getSubTotal() function the value of 2, so it's always going to return 2 * 7.5 instead of 3 * 7.5. Pass orderCount to getSubTotal() instead, like this: getSubTotal(orderCount).

Next, you're going to want to have 3 calls to the takeOrder() function so that you actually set the order count to 3, because right now it's being set to 2 as you're only calling the function twice.

If this solves your question, please be so kind as to mark this answer as the solution to your question. Thanks :slight_smile:


#3

I didn't realize a variable could be an argument. But shouldn't the formula do order count +1? So if I have 2 orders, it should have orderCount = 3 whereas it is still counting 2 and giving me a result of 15. Or maybe I misunderstood? Updated code below:
...
var orderCount =0;
function takeOrder(topping, crustType) {
console.log('Order:' + crustType + ' pizza topped with ' + topping);
orderCount = orderCount + 1;
}
function getSubTotal (itemCount) {
return itemCount * 7.5;
}
takeOrder('bacon', 'thin crust');
takeOrder('ham', 'thin crust');
console.log(getSubTotal(orderCount));
...
and this is the result I get
Order:thin crust pizza topped with bacon
Order:thin crust pizza topped with ham
15


#4

Add another call to takeOrder().

You're only calling it twice, so orderCount will only be equal to 2, so you'll only get 15 as the answer.
Adding another call to takeOrder() means that orderCount will be equal to 3, so you'll get 22.5 as your answer.

If this helped, please be so kind as to mark the answer as the solution to your problem. Thanks :slight_smile:


#5

Ok now I get it! orderCount=orderCount+1 is just telling the script to count the lines that have the function called, so that it's 1+1+1 etc. Thanks a lot!!


#6

Pretty much! Every time you call the function, it adds one to orderCount. So if you call it three times, it adds 3 to orderCount.


#7

help please? I'm very confused about calling and stuff. I dont even know what I dont know.
this is what I have so far??

function takeOrder(topping, crustType)
{
console.log('Order: ' + crustType + ' crust topped with ' + topping);
}
var orderCount = 0;
orderCount = orderCount + 1;
takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

the instruction was to set orderCount equal to orderCount plus 1, so that each time the takeOrder function runs, 1 is added to the orderCount. im so lost! if you know what I need to do please help :frowning:


#8

You want it to add 1 to orderCount every time you call the function. This means you need to put orderCount = orderCount + 1; inside your takeOrder() function.

But please do not post questions on this thread, please make your own as this one is for another person.


#9

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