6. Don't repeat yourself (D.R.Y.)


#1

Hello out there,

I've been having a really rough time picking up/understanding this JavaScript course. This particular section has me extra confused. I've been trying to get it right for days? Can someone help? I feel like I'm reading a foreign language. I tried getting hints from another post similar to this one, but nobody gave a solution.

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


#2

Price should be the argument for the orangeCost function.
Also, when you pass orangeCost() put 5 within parentheses. Final code should look like this:

If that doesn't work then please let me know.


#6

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

orangeCost(5);


#7

Thanks for your help. That is the correct answer. I'm trying now to understand how it works.


#8

@corpsepose83 I'd posted an explanation about what's going on but it's disappeared, but any part in particular that still confuses you?


#9

Hey, yes I saw you replied and then your post did disappear. I don't have any particular questions regarding Javascript. I've been bouncing back and forth between the javascript course and jquery. I guess for me when I'm reading what they are telling me it computes in my brain like math problems in a foreign language haha, if that makes sense... and I'm not good at math. Sometimes I'll read it over and over and still can't figure it out. I'm just looking for a way to make it easier for me to learn.


#10

@corpsepose83 Oh good, I'm glad it to hear I wasn't seeing (er, not seeing) things :joy: Hopefully it was of some use, while it was up anyway :grin:

I know exactly what you mean! Math, and in a foreign language! I do think the way the course is set up some lessons have a smooth progression while others seem all over the place. Plus supplementing studies with the internet means being introduced to a whole host of other concepts we haven't even begun to learn! :scream:

Definitely also for me (although it makes me feel very slow) an important factor seems to be time... Time in between not knowing anything and the next time of not knowing anything seems to make the next time a bit easier somehow. I've taken a doll's handful of programming classes with months or years in between and find the feeling of "learning despite myself" lessens slightly with each new experience. :sweat:


I started this course because it was listed among Hack Reactor's free prep recommendations along with Eloquent JavaScript (which was actually their first choice, but I know from studying Ruby last year that solely following along with a book isn't for me). After I got deeper into the track I started reading the book too.

While it does go in a different order (for example, it introduces recursion early on...I don't want to talk about it, okay?! :tired_face:) and I think I might have been lost were that my first and only ever programming resource, I have found it helpful to see the same lessons in a different perspective, even if I don't necessarily understand it right away. Also that its intended audience is beginners means there won't be any super advanced code to explain the basic concepts we're learning now...


I also borrowed JavaScript & JQuery (Jon Duckett) based on javascriptissexy's "syllabus" recommendations (two books were recommended, and this was the one for "visual" learners. Well, I don't know about that learning style for me but I thought it might be nicer to look at :joy:)

I do like the way it is laid out, which is very neat and readable. Again I am doubtful about how I'd have felt if it were my only resource, but as a supplement to this class I find it very helpful. Some examples of what it looks like:

It is geared towards creating an interactive website though, so the focus is a bit different from this class (although it is nice to see something "irl" we can make with what we're learning).


Anyway, sorry for the long reply! Hopefully I don't sound like I'm just shootin' the breeze here. I also find programming can be confusing... I found these resources helpful and thought you might too. :slight_smile: Let me know what you think!

frisby


PS: one more thing: I also like MDN's A re-introduction to JS as a quick reference that's always on hand, especially if I've forgotten something or if we're introduced to a new definition but I have no idea what it means.


#11

thanks buddy. it worked


#12

var orangeCost = function (price){
var val = price * 5;
console.log("The 5 oranges are going to cost you" + " " + "£" + val);
};

orangeCost(5)

Hi All,

I put this in the exercise and from what I can tell it is right and produces the right answer of 25 but keeps throwing an error back saying it needs to result to be 25
What do I do !!

Thanks


#13

Hi, I think in your code is missing VAR, the error pop out Undefined orangeCost.... Following code was correct one. Any way thanks contribution.

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


#14

what is wrong with this?
var orangecost= function(price){
console.log(price*5);
};
orangecost ("5")


#15

don't use "" for integers.


#16

Aaahgk!! slams fist into mahogany table

I WAS SO CLOSE!!!

I was reading it, trying to understand exactly what they were talking about. If it's one I thing I hate in a math problem... it's a poorly written math problem.

Also, apparently this works too...

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

orangeCost(5)


#17

Oops, try again. It looks like you didn't define orangeCost.
what's meaning?


#18

This didn't work for me. It stated that the variable "Price" hasn't been established or it can't find that variable


#19

Nvm I got it, thanks


#20

Why did you use 5 again in your last line ?


#21

this one worked for me on first try!..
i know there are several ways to arrive at the same answer so please let me know if i did wrong

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