6. d.r.y.?


Could someone please show me an example of how I should write this code?



What is the name of the course?


Which code do you want to write?


Can someone please show me an example of how to do it?



How can we write an example on it if we don't know which course you are talking about? Please give us the link to this exercise so we can help you.


Here is a screen shot of what I have so far:



Well, basically, you need to create a function called orangeCost that takes in parameter the cost of oranges. It will log to the console the cost of oranges times 5 because we want to print the cost of 5 oranges. Your code is a good start, just remove the extra () next to orangeCost. Optional parameters are defined next to function.

Here's a syntax refresher for a function

var functionName = function (optional parameters) {
// the code gets executed here

This is a syntax refresher for logging things in the console

console.log(/* you can print what you want like operations */);


I put this:

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



Remove this line. This is for the user to decide what will be the price of the oranges by calling the function giving it its price as parameter.


Here is my code-
var orangeCost = function(price) {
var cost = price*5;
The D.R.Y principle is really important in programming.So function is very important for this.
you can undestand by simply thinking that what if price is always changing.Without function you have to write many lines of code.So dont worry function is here.With function everytime i just change the price by call it.Like...
First orange cost was Rs5.then 6 then 7. ok?
any question?


No questions. Thanks so much!


This code works fine, but if you try something like:

console.log("Oranges are now " + cost);

it stops working?


If it helps, I did this and it seemed to work.
I've also added comments in between the lines to give you (and myself) and idea of what the logic was for this...

// created a variable orangeCost to be a function.
// function has price in the parameters to let the code know that there some input will be required down the road when running this function.
var orangeCost = function(price) {
// declared the price variable, did not add a value as this will always change
var price;
// declared the orange variable with a value of 5, as this will always be 5
var orange = 5;
// I want this function to print out the the price * 5 oranges when this function is run.
console.log(price * 5);

// calling the orangeCost function.
// this is where the orangeCost = function(price) thing comes into play.
// I am now calling the variable, orangeCost I created with the price that will always change.
// this will now print the price * number of oranges


The part that gives an error is if you put a string inside your console.log. For example:
console.log("The total cost is "+ price*5); //This will not work
console.log(price*5); //This will work


The "Don't Repeat Yourself" principle is from a book "The Pragmatic Programmer". The book is about general principles like fixing broking code as you encounter it. ("Fix Broken Windows"). An other general topics.

(I still need to procure myself a copy..)

Wikipedia has an article describing the principle:

Alongside a language syntax, programming principles are pretty important too. You can google topics like "Gotoless programming", "Structured programming", etc. if you want to get a feel on how far back in time the ideas have been threshed out and taught.