Could someone help me with 6 Don't Repeat Yourself?


#1


SyntaxError: Unexpected token *


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


#2

I have no idea what I'm doing, I left for a month and came back. I tried backtracking but nothing makes any sense.


#3

You basically wrote it using common sense, but unfortunately your syntax isn't what JavaScript would like it to be.
First, you should start by just defining the function. You would write it almost exactly the way you did it, except for you wouldn't include the * 5 inside of the parenthesis...
The parenthesis are for setting up how many parameters there will be in the function. It honestly doesn't matter what you call them, but the lesson would like you to call it cost.
After setting parameters, inside of the function you would set a variable called cost1 and assign it cost * 5.
The reason this is the way is because it creates a NEW variable for you to print to the console so after defining that variable, you would just print cost1.
The reason you wouldn't print orangeCost is because that's the function and as long as you are printing the final cost, (cost1), to the console, you only need to call the function.
Finally, the last thing you do is call the function which is simple. To call a function you just type the functions name and pass it the argument. In this case, the lesson wants the argument to be five so your last line would look like this:
orangeCost(5)
That's saying orangeCost of five.
Then it goes through your function, multiplies it by five and assigns that number to cost1, then prints cost1.

I hope this helps!


#4

The basic function expression statement consists of,

  1. a var keyword
  2. an identifier name
  3. an assigment operator
  4. a function keyword
  5. a parameter enclosure (parens)
  6. zero or more parameters (a variable)
  7. an opening curly brace to define the function body
  8. a closing curly brace on the function body followed by a semi-colon
  9. a block of code statements inside the function body
  10. a return keyword in the code block if data is being returned

var my_func = function (foo, bar) {
    console.log(foo + bar);
};

We execute the function by calling it...

my_func("pine", "apple");

// pineapple

In the above, pine and apple are the string arguments being passed in to the function. They will be referred to inside the function by foo and bar, respectively, since they are the declared parameters. These are local variables, as you will learn soon if not already.

In JavaScript we cannot write an expression as a parameter, only an argument. An expression is anything that yields a value. price * 5 is an expression. As such, it is an illegal parameter and should raise an exception.

my_func(2 * 2 * 2, 3 * 3);
// foo == 8; bar == 9
// 17

So it is 8 and 9 that actually get passed. The expression is evaluated immediately, and never seen by the function we just called.

One last comment before we reconstruct your code. It pertains to function identifiers.

console.log (orangeCost)

The above will display, [Function] or something similar. The identifier refers to only the string that the code is written as. To execute a function we invoke it with (), as in, orangeCost(). Okay, let's reconstruct from what we've discussed above.

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

#6

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