How to do Introduction to Functions in JS Les. 6 Don't Repeat Yourself

#1

I need help on Introduction to Functions in JS Les. 6 Don't Repeat Yourself because I am very lost and have no idea what to do could somebody please walk me through it or give me the code please?

Here are the instructions:
Don't Repeat Yourself (D.R.Y)

The D.R.Y. principle is really important in programming. No repeating!

Any time you find yourself typing the same thing, but modifying only one small part, you can probably use a function.

The 'small part' that you find yourself modifying will be the parameter. And the part that you keep repeating will be the code in the reusable block - the code inside { }.

Instructions
You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!

You want to declare a function that calculates the cost of buying 5 oranges.

You then want to calculate the cost of the 5 all together.

Write a function that does this called orangeCost().

It should take a parameter that is the cost of an orange, and multiply it by 5.

It should log the result of the multiplication to the console.

Call the function where oranges each cost 5 dollars.

Hint:
What is the one bit of input that changes each time? It would be the price. So give your parameter the name price. And when you call your function, put in a number for the price to see how much 5 oranges cost.

#2

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

the instructions

You want to declare a function that calculates the cost of buying 5 oranges.
You then want to calculate the cost of the 5 all together.
-1 Write a function that does this called orangeCost().

``function orangeCost() { }``

-2 It should take a parameter that is the cost of an orange, and multiply it by 5.

``````function orangeCost( cost ) {
//Begin of FUNCTION-BODY
//orangeCost =function= has 1 PARAMETER cost
//this cost-PARAMETER is used as a -local-VARIABLE
//throughout the FUNCTION-BODY

cost * 5

//End of FUNCTION-BODY
}``````

-3 It should log the result of the multiplication to the console.

``````function orangeCost( cost ) {
//Begin of FUNCTION-BODY
console.log( cost * 5 );
//End of FUNCTION-BODY
}``````

-4. Call the function where oranges each cost 5 dollars.
We want to call/execute the orangeCost function
and the orangeCost function was defined
as having 1 parameter
we have to provide 1 argument
which in our case is a number VALUE of 5
like:

``orangeCost(5);``

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

#4

WHAT!?!?!?!?!?!?!!?!?! This still; makes no sense to me but it worked so THANK YOU!!!!!!!!!

#5

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

#6

JavaScript: Unit 2, Lesson 6

This one is understandably confusing. The question and instructions are a little ambiguous and strangely worded at points. Let's see if we can break down what they're asking for and communicate it more clearly...

Instructions

1. Declare a function that calculates the cost of buying five oranges. (Don't let them throw you off when they say "declare a function..." insted of saying "declare a variable..." They mean 'declare a variable that equals a function.')

2. You then want to calculate the costs of the 5 all together.

3. Write a function that does this called orangeCost().

4. It should take a parameter that is the cost of an orange, and multiply it by 5.

What this means to you:

orangeCost = the total cost of all the oranges you buy.
price = the variable cost of one orange.

You want to produce a formula that adjusts the total orangeCost accordingly every time you change either the price or the number of oranges. In this case, we know the number of oranges (5). So your code up to this point should look something like this...

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

It gets a bit dodgy after here...

5. It should log the result of the multiplication to the console.
This could mean any number of things. Do they want you to write a sentence? What is 'the multiplication'? What are they asking? What do I do? Aaaaaahhh?

All this means is take 'val' which is the 'result of the multiplication' and put it into the console.log.
Like so: `console.log(val)`

So now our code should look like this:

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

6. Call the function where oranges each cost \$5

Oh god. Not another 5. Here we go again. Don't let it trip you up. When they say 'call the function', they just mean write that original variable: orangeCost. Along with the function parenthesis and what's inside it: (price). We now know the price of an orange is \$5, which means we can put that into the price space like so: `orangeCost(5);`

Your final code would look like this:

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

orangeCost(5);``````

That's it. 5 oranges * \$5 = \$25. Boom! Oranges for days! Wait a minute... \$5 for a goddamn orange? That's totes ridic!

#7

Think about a function as a physical box that youâ€™re able to put something into it, it does something to that thing you put into it and then it spits out that thing after itâ€™s done what ever youâ€™ve told it to do to it.

You put something into that box (input), something happens to that input (the code inside the {curly brackets}) and something comes out on the right side (output).

In this case, the input is the price of a single orange (orangePrice). The code inside the {curly brackets} is that actual action on the input of the orangePrice and the output occurs when you call on the input of orangeCost(1).

Breakdown explanation of code is below:

var orangeCost = function(orangePrice) { // Assigns the variable orangeCost (think of it as the calculator) and telling the computer itâ€™s a function thatâ€™ll take the input of orangePrice and do something to that input

console.log(orangePrice * 5); // This is the that part where youâ€™re telling the computer what to do to the input (orangePrice). In this case, multiplying it by 5 oranges

orangeCost(5) // Setting orangeCost (the price of a single orange) to \$1 so that the total cost is \$5 (the computer multiples 5 oranges x \$5 = \$25 total