D.R.Y help


#1


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

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

I get the above error message but it is returning 25.


#2

@lontronix,

The exercise only expects a number Value to be returned, NO extra text's !!!

### 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".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#3

hello your code isn't correct you should ha something like this

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


#4

@je22
you are forgetting to use the console.log() Method.....

As is stated int the Instructions
display to the console.........!!!!


#5

it works like that for me


#6

Thank you, this was very helpful! :slight_smile:


#7

Thank you :slight_smile:


#8

Thanks for all the help. I am working on the same lesson. I used je22's code and it worked.. I just don't understand why.

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

Unless I am misunderstanding, I don't think I defined the price.. So I don't know how it calculated it to equal 25.
The first line of code defines orangeCost as equal to the function and parameters (e.g., price). Shouldn't there be an additional line to define the parameters before the opening curly brackets?.. Just trying to get my head around why it is working and why I do not need to define the parameters in this instance. Thanks!


#9

@lontronix @je22 @leonhard.wettengmx.n Could you help me How to solve this or in where I am making mistake.

Thanks!


#10

i think there is a ; after the ] and it s not necessary

2016-07-08 17:31 GMT+02:00 Konstantine Gegeshidze <
codecademy@discoursemail.com>:


#11

and when you call your function it'snt 5 but price


#12

@javaninja17715

You will have to read, and re-read
https://discuss.codecademy.com/t/d-r-y-help/48842/2?u=leonhard.wettengmx.n

Instead of using the parameter price as a variable in your function-body
you are introducing a string Value "price" in your function-body.....


#13

I don't know where I am going wrong,having checked it many times I am getting same error.Please help


#14

@palakchopra

Try using

var orangeCost = function (price) { };

#15

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