D.r.y


#1

Ok, I've gotta admitte that I don't know if I learn all this course has offered to me, but it's true that's repetition is a childish way of learning. Ohhh, I'm digressing !! ha ha ha

Let's do what matters!!

var calculate = function(orangeCost) {
var orangePrice = 5;
console.log("orange price costs " + orangePrice * 5 + " by week ");

};
orangeCost function("orangePrice: " + orangePrice * 5 + " by week ");

SyntaxError: Unexpected token function
Oops, try again. There was a problem with your syntax.
PS.: when I enclose curly brackets occur the same message above.


#2

I think the main problem is your trying to call your function with the wrong syntax...

in order to call your function ( called calculate) you should simply use calulate()..

I'm not sure what you're doing with this line:
orangeCost function("orangePrice: " + orangePrice * 5 + " by week ");


#3

If I delete the line you didn't understand, occur the following message:

Oops, try again. It looks like you didn't call the orangeCost function. Make sure to call it and pass it a price!


#4

I'd like to oppose "childish way of learning" statement, since it helps. Believe me, it really does. You need to get used to feel comfortable with the code you're writing. You need to understand how it works and why it works. Memorizing is not an option.

Now, on your case. I'm not even sure where to start. You have one parameter orangeCost set in your function calculate which you don't even use. Then you're calling your function wrong...

This is a working piece of code, it's fairly simple and you should be able to understand how a simple function like this one works.

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

orangeCost(5);

Don't proceed to next lessons without completely understanding this one.

Good luck!


#5

Oh, I appreciatted your attention, really. Mainly the sentence " Memorize is not an option". I didn't know that. However, I was bold and have decided to follow my own way with this:

var calculate = function(orangeCost) {
var orangeCost = 5;
console.log("Orange price costs " + orangeCost * 5 + " USD " + " by week");

};
calculate(orangeCost * 5);

LOG OUT WAS:

Orange price costs 25 USD by week

Oops, try again. It looks like you didn't define orangeCost.

Then, Could you help me to define orangeCost ??

thanks, man


#6

Nah, man, I will pass this time.

:see_no_evil:


#7

orangeCost( 5 );

//Console goes back up to the function

var orangeCost = function( 5 ) { //Replaces the parameter "price" with the number 5.

var val = 5 * 5 ;

console.log ("orangeCost" + 5); //Console outputs "orangeCost5"
//the console is expecting the number 25, but got "orangeCost5"
The second function looks like this:

orangeCost( 5 );

var orangeCost = function(5){ //Replaces "price" with the number 5.

console.log(5 * 5); //Logs the number 25 to the console.
//the console is expecting the number 25, got "25", Lesson Passes.
};


#8
var calculate = function(orangeCost) {
    var orangeCost = 5;
    console.log("Orange price costs " + orangeCost * 5 + " USD " + " by week");

}; 
    calculate(orangeCost * 5);

Ok first read the instructions carefully there are two naming conventions here so that the test function is able to check your code. The first is that your function should be named orangeCost (not calculate) and the second is that your output should be just a number. Actually your output message is pretty good and you should keep that up but in this case the requirement is just to use a number. Another thing is that by using a parameter in your case orangeCost (which needs to be renamed to something else as this should be the name of your function) you declare a local variable and by using functionName(input) outside of the function you do something like

//PSEUDO-CODE NOT RUN THIS
var name = function(){
   var parameter = input;
   ...
}

but this is just an image to understand what is happening, you don't need to do this your own and literally like this:

var orangeCost = 5;

Is it clear what I mean? Also in this statement:

calculate(orangeCost * 5);

you have no knowledge what orangeCost is (better the interpreter doesn't know it because it is local to the calculate function) and therefore you get an error message.

Hope this makes a sense, if not respond on it.

@arijhoan

var orangeCost = function( 5 ) { //Replaces the parameter "price" with the number 5.

NO. Parameters need to be variable names so using a value here will just produce an error.

console.log(5 * 5); //Logs the number 25 to the console.

Yes this might work but it is actually cheating on the very concept of functions as this will not work for any input other than 5.


#9

I'm having trouble understanding as to how your code works and I have been at for a while and I still don't understand can you please explain?


#10

I agree, let's oppose the "childish way of learning", yet do so with the utmost understanding. Repetition does not foster understanding, it builds familiarity. The most conditioning way for anyone learning to code is to instead start a project of their own. This is because if you want understanding, you can't be told what to do, you have to do it on your own.

You cannot learn what you're coding or you're doing through repetition. I would like to pose the opposite scenario, that understanding does not come from repetition, but instead significance. If you're looking to understand something, create a project, make it unique, something important to you or something you're willing to invest in whether large or small.

All true and all is found in the significance aspect of your work, though comfort is also found in repetition, it's where you really learn what works best in the significance. Learning what practices or methods only applies when you're working on something with significance. If you want to grow, it's very important to mix in a few projects of your own with your exercises, and all they need to be is a simple file where you write code that you thought of.

For example, I have a single php file that I am practicing with. It has one line of text "this is a header" which I do different things with, count characters, letters, reverse it, below it is an empty simple form where I practice with form validation, at the start of the file are cookie and session calls.

All within a single file. I've also got separate documents which I've begun writing my own comprehensive notes on the basics of javascript, php, etc, for my own purposes, or potentially others.


#11

This is basic function. A function runs a block of code included within it, of any size, though it's good not to make them exceedingly large and to break down tasks into smaller functions.

  1. This function takes a parameter 'cost', which is a variable value that the user can pass into it, the function works with this placeholder denoted with the same variablename throughout the script (because the parameter name becomes a local scope variable = it only exists within the function).

  2. The user passes in a cost, so the first thing the function does is multiply cost by 5 and assigns that to the cost variable (it does this so that it updates the cost, otherwise it wouldn't be saved, and there's no need to make another variable. So it's doing work with cost, then applying the new value.

  3. Console.log() is a function which outputs a string to the console. In this function we see the parameter 'cost' one last time as it's now printing the value to the console for us to see the change.

now when you are defining a function (key word defining, or creating your structure for a function) You're not calling it to run, you're simply stating how it'll work, you call (or ask it to run) the function to run later on by writing it's name which is between the function data type, and it's brackets.

function empty(optionalParameter) {
    console.log(optionalParameter);
}

empty('Send this string to the console');

Here is a simpler block of code, don't focus on the names as much as their placement, purpose, and role. Hope this helps.


#12

You need both. You're actually learning languages so if you don't know you're vocabulary you can't speak it, but just from learning the vocabulary you most likely won't get fluent in the language.


#13

Debra. I ran your code and it worked. It was only slightly different than mine. b I am still questioning how it read and it accepted the answer since it is requesting to say "Oranges $25". At least thats what i understood in the problem.


#14

I am attempting to figure out how come I get a "unexpected token *" error with this code:

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

I was able to figure out the correct function, but I still do not fully understand where this code goes wrong.


#15

var orangeCost = function (price * 5){

a paramter is the name of a local variable of the function with the special ability that it can get values assigned by entering them in the () of your function call.

so in a function

var name = function(x){

}

x would get a value of e.g. 3 if you call name(3).

So if you compare it to your code you neither call the function (instead you call price which is not a function) nor is the parameter a variable name (in your case it is a value (undefined) variable times 5). So as there is no good reason for anything else then a variable name at the position inside the () after function you get this unexpected token error.


#16

Thank you for the explanation. It definitely helps clarify.