最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Codeacademy JavaScript - Section 2-1 - Stack Overflow

programmeradmin2浏览0评论

I took an AP puter science course a few years ago, I learned Java from it. I'm trying this Codeacademy now and I'm puzzled by this one question, can anyone explain this to me? Here is the question:

You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!

  1. You want to declare a function that calculates the cost of buying 5 oranges.
  2. You then want to calculate the cost of the 5 all together.
  3. Write a function that does this called orangeCost().
  4. It should take a parameter that is the cost of an orange, and multiply it by 5.
  5. It should log the result of the multiplication to the console.
  6. Call the function where oranges each cost 5 dollars.

Here's my code:

var getCost = orangeCost (costOfOrange) {
    console.log(costOfOrange * 5);
};
getCost(5);

I believe it follows the syntax showed in earlier problems, but I'm getting this output:

SyntaxError: missing  before statement
Oops, try again. 
It looks like your syntax isn't quite right. 
Feel free to peek back at earlier exercises if you need help!

I took an AP puter science course a few years ago, I learned Java from it. I'm trying this Codeacademy now and I'm puzzled by this one question, can anyone explain this to me? Here is the question:

You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!

  1. You want to declare a function that calculates the cost of buying 5 oranges.
  2. You then want to calculate the cost of the 5 all together.
  3. Write a function that does this called orangeCost().
  4. It should take a parameter that is the cost of an orange, and multiply it by 5.
  5. It should log the result of the multiplication to the console.
  6. Call the function where oranges each cost 5 dollars.

Here's my code:

var getCost = orangeCost (costOfOrange) {
    console.log(costOfOrange * 5);
};
getCost(5);

I believe it follows the syntax showed in earlier problems, but I'm getting this output:

SyntaxError: missing  before statement
Oops, try again. 
It looks like your syntax isn't quite right. 
Feel free to peek back at earlier exercises if you need help!
Share Improve this question edited Nov 19, 2012 at 23:10 Madara's Ghost 175k50 gold badges272 silver badges314 bronze badges asked Nov 19, 2012 at 23:07 user1837204user1837204 612 silver badges7 bronze badges
Add a ment  | 

10 Answers 10

Reset to default 3

Function definition in JavaScript can take one of two major forms:

function funcName(param1, param2, ...) { }

Or

funcName = function(param1, param2, ...) { }

Your example doesn't follow either. You probably want:

orangeCost = function(costOfOrange) {

It looks as simple as you forgot the function keyword and there's no orangeCost after the =:

var getCost = function (costOfOrange) {
    console.log(costOfOrange * 5);
};
getCost(5);

Did this help much?

I have solved mine using the following code:

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

I believe it was asking to verify that the orangeCost be the function name, that's why it refuses any other thing though there's other ways to do this. Hope this helps as I was also surprised by this question and spend sometime :)

var orangeCost = function (price) {

    console.log(price * 5);
}

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

here is my code you could refer it

Check it out dude. What you have done is that you are using orangeCost in place of function keyword.

This is what you should have done:

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

orangeCost(5);

Follow the instruction below:

var  functionName = function(parameterName){
//function definition.what you want to do 
}

In your case the code may be like this:

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

easy. Just create the function and evaluate it when printing out

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

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

orangeCost(5);

You shoud call orangeCost value instead of "price" which is the function name. there it goes.

发布评论

评论列表(0)

  1. 暂无评论