dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: Simple function outputs three values when only one is expected!

  1. #1
    Join Date
    Apr 2014
    Posts
    4

    Simple function outputs three values when only one is expected!

    Hey folks,

    I'm just diving into Javascript on Code Academy and I have a real quick question about the code I pasted below. It worked and I passed the section but I'm wondering why I am seeing an output of 25, 25, 50. I expected to just see 25. Something funky happening here?

    I'll past the instructions here:

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

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


    code:

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

    };

    orangeCost(5);


    Again the output I see is:

    25
    25
    50

    Not a huge problem but I'm trying to grab all the details as I go along.

    Cheers for the help!

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    Is there any other code or HTML on the page since the function you gave only returns 1 value?

  3. #3
    Join Date
    Apr 2014
    Posts
    4
    The only code in the editor was what I pasted in my thread. Most curious.

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,031
    I feel like there has to be some other code, or another step to this. You say the output is '25, 25, 50', which of course is 3 values. The function is only called once and so there would only be one value.

    If I take the exact code you posted and run it I simply get '25' and nothing more logged to the console. As far as what you posted in this thread goes there are no errors, syntax, logical or otherwise.

  5. #5
    Join Date
    Apr 2014
    Posts
    4
    Funky! Well at least my code is correct. Thanks for the replies <3

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,435
    Question: Did you clear the contents of the console.log before running your program?
    It may be showing the results of earlier runs.

  7. #7
    Join Date
    Apr 2014
    Posts
    4
    Nope because I don't know how to do that, ;P. What's the command for it?

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,435

    Lightbulb

    Quote Originally Posted by uncleahwei View Post
    nope because i don't know how to do that, ;p. What's the command for it?
    f12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles