6.Don't Repeat Yourself (D.R.Y)


#1

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

Cant think of anything. Help please with this code. If you can otvetet in Russian, do it yourself.


I dont know where is the problem
#2

@denitargaryen,

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

@denitargaryen,
After you have read the Function talk
Please read
https://discuss.codecademy.com/t/window-freezes-on-6-13-when-trying-to-submit-code/3377/4?u=leonhard.wettengmx.n


#4

couple mistakes:

  • in your console.log output, you don't need quotation marks.
  • last line (orangeCost(5)) has bad syntax... you need to add something.

#5

Hi! I stuck in same part.
Why do I get two answers?


What is wrong?
// I apologize for my poor english


#6

@foxhound86
You will have to use the return statement instead of the console.log() method
and
you only have to return price*5
no other text....


#7

@vucurs I used the exact same code as yours, with same results. Based on the instructions so far this is logical for us, but the answer of @leonhard.wettengmx.n is the one they want:

PS return is explained in the next exercise :joy:


#8

@stafmans
as you are using the return statement the normal call would have to be
console.log( orangeCost(5) );


#9

Tx. I couldn't make this up from the instructions, but it seems logical from what I want to do: print to screen.


#10

@stafmans
Keep this in mind while doing the course

It is the Javascript interpreter which is running in this course-environment,
it display's the last non-displayed action...

You will also encounter this, when you are doing a return-statement
as last action...
You could counter this by using a
console.log( " " ); as last statement....

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


#11

@leonhard.wettengmx.n Now you made me curious, so I went back to try. Indeed there's no output when I finish with another console.log() statement. So console.log(organgeCost(5)) is the only proper syntax.


#12

@stafmans
I think you could also replace your 2 console.log() Methods
with 1

console.log("== My End ==');

#13

Here is my code

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

};
orangeCost(2.56);

#14

It's good idea if you add the missing semicolons as well to your return statement, end of the function and also the end of the console.logs.


#15

Hi @amarillia I deliberately don't. It's a course and I want to find out what I think of those semi-colons. Whether I like my code with or without. Besides this I'm also curious - after my PHP course :slight_smile: - when things work without semi-colons.


#16

@stafmans
@amarillia
I would read some article's found with google search
== discussions / opinions ==
javascript when to use semicolon site:stackoverflow.com
http://stackoverflow.com/questions/2399935/why-use-semicolon
http://stackoverflow.com/questions/3781406/javascript-variable-definition-commas-vs-semicolons
== the Book ==
semicolon site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar


#17

It doesn't want you to return any verbiage just the calculated number.


#19

Here is what I put for and it came back correct.

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


#20

you can use this


#21

We haven't covered 'Return' yet so wouldn't this simply be the answer they were asking for?