D.r.y


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/1/1

i am stuck on this one it says im doing it right but it is not printing anything in the console

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

orangeCost("5")


#3

Thats because you're not printing anything to the console.

console.log(orangeCost(5)); notice no " " around 5 because its a number not a string.

Another way you could have done this is

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

#4

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

The instructions do not ask us to return anything, just log it in the function.

console.log(price * 5);

#5

Hey mr.taco... I feel your pain on this lesson for sure. I was having a tough time figuring it out too. After trying it on my own a couple times I looked back on some notes I took and went through it step by step but I believe this is another way to do it... you were extremely close by the way! Just an extra step and you would have nailed it.

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

The output will print 25 in the console.
1) Define your variable: given in the instructions... var orangeCost

2) Following the variable we know function will be the price of a single orange. This is needed because the only thing changing will be the price since we always purchase 5 oranges.
var orangeCost = function (price) {

3) Now we need to identify our parameter which will be the cost of a single orange, and multiply it by 5. This means we need to identify another variable which we will call val.
var val = price * 5;

4.) The instructions then tell us to log (or console.log) the result of the multiplication into the console. Now that we defined our new variable val = price * 5... log our variable val.
var val = price * 5;
console.log(val);
};

5.) Last step will be to Call out the function where each orange cost 5 dollars. This is the easy part now that we determined the other information.
orangeCost(5);

6.) Notice the number 25 printed out in the console. Now that we have everything logged, we can change the price to whatever we want since price is changing but our quantity of oranges is not.

Hope this helps and I am not too late getting an answer!


#6

Helped me quite a lot. Good description, a lot clearer for me now =)


#7

I type exactly like yours, but I found a problem.
if I coded like:
var orangeCost = function(price) {
var val = price * 5
};
console.log(orangeCost(5));

then the result showed "undefined" (please noted that codecademy still have me pass this task)

if I coded like:
var orangeCost = function(price) {
var val = price * 5
};
orangeCost(5);

then the result showed nothing. (still pass the task)

Did I miss something?


#8

Hey coderrockstar06513... the only thing I see is not logging the new variable val (value). So using console.log for the variable val.

I believe it should look like this... after you define your orangeCost variable and function:

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

So on the first set of code you displayed in your comment, we don't console.log(orangeCost(5)); after the curly brackets. What we do need to console.log is the new variable called val = price * 5;. That way the computer knows to store that new variable in order for the function to work . So below the line of code that reads var val = price * 5; (don't forget the semi-colon..I forget it all the time, still working on it haha) we need to log it using console.log. Which will look like this:

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

Next, the problem asks for us to "call" out the function. Meaning all we have to do is type our variable orangeCost(5);
This will then give us an output of 25. On your first set of code, you did a log (console.log) of orangeCost which isn't what the computer was looking for in order to complete the problem.

For your second set of code in your comment, you pretty much nailed it! But the only thing that was missing was logging the new variable val (which goes below the var val = price * 5;) and the semi-colon after 5. Other than that, its correct because you called out our variable orangeCost(5); on the last line of code.

So it should look like this:

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

I apologize for this being really long but for me it always helps going in depth with thing. Hope this helps and I answered your questions!


#9

thank you this was very informative