6. d.r.y


#1



https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/1?curriculum_id=506324b3a7dffd00020bf661#


I don't know what I did wrong, but every time it shows :
Oops, try again. Your code doesn't look quite right. Check the Hint if you need help!
Can you please help me and also tell me where I can find the correct code without having to ask you guys for help?

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

price(5);

P.S.: I don't really understand function- Couldn't you just do

console.log(5*5);

for the above example?


#2

@digitalrockstar35435,

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

I put in the following and it says it's correct:

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

But I've changed "price * 5" several times to different things to experiment, and they're all "correct." So I don't know which one is actually correct and why.


#4

@mlemming,
With

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

you have created a orangeCost() function which takes 1 parameter price

In the function-body you are using this price parameter
as a variable,
You multiply this price variable with the number Value 5
and then use a console.log() Method to Display the =Result=.

Now if you call this orangeCost() function
and as the orangeCost() function was defined as having 1 parameter
you have to provide 1 argument
in our case that would be a number Value 5
thus
orangeCost(5);


#5

Thanks a lot! I got it now.


#6

your are suppose to call orangeCost("5"); instead of price(5);


#7

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


#8

why should we call prive("5")
becuase as far as i know numbers shouldnt be need to be defined


#9

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

orangeCost(5);


#10

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

orangeCost(5);


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

worked for me though I'd love to hear some feedback as to why I need to put (val).


#12

Get rid of the console.log();

When I first did it I had:

var orangeCost = function(price){
var total = (price * 5);
console.log("The total oranges cost $" + total);
};

orangeCost(2.99);

because if the orange prices were changing then I could put whatever number in there to calculate it so I could change the variable. So I put in "2.99" to test it, but no, it had to come out to "25" total.

The log printed:

The total oranges cost $14_ish_
The total oranges cost $25

with an error message along the lines of it didn't come out to 25.

So I was really confused that it was saying it twice. I think that the lesson's code was was looking for 25 and somehow my code screwed it up. Although the second that I made the "price" equal 5 after reading the forums a little and deleting the console.log making my code look like this:

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

orangeCost(5);

it finally ended up working and let me pass.

Also the instructions are incredibly confusing. I think there is a mistake in the first and second "instruction" because 1 and 2 say the same thing practically. Or perhaps they were not clear enough that they wanted it to equal 25 because that is not what I gathered from the instructions. I thought it was asking to make a function that could take any number and muiltiply it by 5 with out an exact outcome.


#13

The code below gets me the result, but I should I be using val instead? The instructions say to use orangeCost as the function and price as the parameter. I'm confused! :wink:

// Declare a function that calculates the cost of buying 5 oranges //
var orangeCost = function (price) {
console.log(price * 5);
};
orangeCost("5");


#14

@byteninja14580

Re-read the function talk
https://discuss.codecademy.com/t/6-d-r-y/39171/2?u=leonhard.wettengmx.n