Help my Introduction to Functions in JS thing number6 Don't Repeat Yourself (D.R.Y) isn't working!


#1

Here is what I have:

var orangeCost() = function (price) {
var val = price * 5

};

orangeCost("$5.00")

but it always says "SyntaxError: missing before statement"
WHAT AM I MISSING? HELP!


#2

should be
orangeCost

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

hi,

I have just completed this task too and did a similar thing to yourself by adding in the dollar sign when I called the function

orangeCost("$5.00")

This won't work as the code currently stands - not entirely sure why - but am sure as we progress this will be explained to us.

Three things (that should help) when I adjusted your code it worked ok:

  1. To get the desired output we just have to enter the number as a single digit when we call the function and the code will work fine.
    // Call the function where oranges each cost 5 dollars. - no need for the dollar sign just the number digit
    orangeCost(Call the function where oranges each cost 5 dollars.)

  2. when i tried your code the way you wrote it when you declared and named your function: orangeCost()
    it did not work you are asking the computer to enter the number via the (price) parameter
    so you do not add the empty ( ) brackets after the function you named orangeCost

  3. remember to print out the message/result to the black console window....
    console.log(val);

var orangeCost = function (price)
{
var val = price * 5;
console.log(val);
};

// Call the function where oranges each cost 5 dollars. - no need for the dollar sign just the number digit

orangeCost(Call the function where oranges each cost 5 dollars.)

note:
if you wanted the result message to show with a dollar sign too you could alter the message line like so:
console.log("$" + val);

When I edited the code this way it displayed the result total with the dollar sign in front.
output: $xx

hope that helps


#4

var orangeCost = function (price)
{
var val = price * 5;
orangeCost("5");
};


#5

@codingbadasssid,

When you use code like your

var orangeCost = function (price) 
{
 var val = price * 5;
 orangeCost("5");  <<==recursive call on the =orangeCost= function
};

you should use

var orangeCost = function (price)  {
       var val = price * 5;
       };
orangeCost(5);

( providing the =orangeCost= function a =number Value= as argument )


#6

orangeCost("$5.00") doesn't work because you're passing the function a String variable. It can't add an Integer to a String like that.

If you want it formatted with a dollar sign, it should be:

console.log("$" + orangeCost);


#7

you can add integers and strings, it just doesn't understand what your doing so "$5.00"+5 would be "$5.005" just like "java"+"Script" would be "javaScript" and "$5.00"*5 would probably be NaN because "$" is not a number.
Also using the $ sign isn't a good idea anyway because the output should be a number so you probably get an error for this. But if it would work then what @wessltov says would probably the way to go.


#8

Thanks for the clarification. Much appreciated.

I think because at the beginning it gets confusing learning new jargon simply differentiating between argument and parameters can easily get misunderstood or be unclear.

var myFunction = function( param1 )
// so this is receiving the argument we want to pass into the function

myFunc(argument)
// so this is what is passed into the function when we call a function

and ...
now you explained that we are passing in a string and not a numeric number it makes more sense too.

I will add these to my notes to remind me.


#9

@awalklearn,

parameter is the receiving variable used within the function/block

--------------------------------------------------

myFunc(argument) 
// so this is what is passed into the function when we call a function

****argument is the value/variable/reference being passed in**


#10

here is an improved version of your code

var orangeCost = function (price) {
var val = price * 5

};

orangeCost(5)

You're problem is you put the orange cost as a string rather than a value, you cannot multiply a string by a value. Another problem was the two parenthesis you put next to orangeCost, this was your original syntax error.


#11

so my code looks like this:

var orangeCost = function (price) {
console.log(price*5);
};
orangeCost(5);

and it works. But I want to know if it's wrong to write the code like that?

Thanks,


#12

It's fine could you elaborate why you think that there is something wrong with this?


#13

because I noticed the others had used var val instead of console.log and since I'm new to coding I thought I might have missed something :grin:


#14

hola como estas ? hello , how are you :wink:?


#15

Of course you could introduce a new variable e.g.

var val = price*5
console.log(val)

but in the end it obviously does the same:

val is price*5 so if you use val it means price*5. As this variable is only used once and never changed in the progress its fine to console.log the value directly and not use a variable.


#16

var orangeCost = function (price) {
var totalCost=price*5;
console.log("Cost of 5 oranges is " + totalCost);
};
orangeCost(5);

can anyone please tell me what is wrong with this code,i get always the same error
"Cost for 5 oranges is 25
Cost for 5 oranges is 25"


#17

@datamaster48140,

The course-checker is looking for the plain value 25
thus you will have to change your console.log()
so that it will only display totalCost


#18

Understood! Thanks for the explanation :smile:


#19

Thank you very much for reply


#20

Why can't I get/see a result with the following code, nevertheless I get a positive answer to start the next lesson?

orangesAmount = 5;
priceOrange = 5;
var orangeCost = function (price){
    var totalCost=orangesAmount*priceOrange;
}
orangeCost("price");

If I change the code to the following I get a double result:

orangesAmount = 5;
priceOrange = 5;
var orangeCost = function (price){
    var totalCost=orangesAmount*priceOrange;
        console.log(totalCost + "$");  
}
orangeCost("price");

Shows me a double result (but at least I see a result now):

25$
25$

With the remark:
Oops, try again. Hmm, it looks like your orangeCost() function doesn't return 25 when the cost of an orange is 5.

I'm wondering why the first code doesn't show me any result. Does anyone have a sollution/explenation/tip?