Solved

Javascript function question

Posted on 2010-08-30
3
310 Views
Last Modified: 2012-05-10
    At the end of this quesiton is  a snippet of code from a JavaScript book.
     Can someone please tell me what "donutString" is? What is its purpose here? What does it do?
     Why is it in parenthesis on lthat first line? I initially thought that meant it was some variable that had been created from outside the parseDonuts function and is thus being plugged into that function. But that is not the case. It was created from within that function and it does not appear anywhere outside of that function. So what's it doing inside the parenthesis? I thought values only go in there if there coming from outside the function?

      function parseDonuts(donutString) {
        numDonuts = parseInt(donutString);
        if (donutString.indexOf("dozen") != -1)
          numDonuts *= 12;
        return numDonuts;
      }
0
Comment
Question by:john8217
3 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 125 total points
Comment Utility
donutString is the name of the variable that is sent along with the function. It's basically the value that you want to manipulate. When you make the call for the function: parseDonuts("15"), donutString will have the value "15". Please note that the value "15" is different from the value 15. One is a string, the other is a number.
It then assigns to a new variable the "15" converted into a number. I'm not sure what it checks for when it checks for indexOf("dozen"), but when it isn't, it multiplies by 12 and returns that value.
0
 
LVL 2

Assisted Solution

by:Tiller79188231
Tiller79188231 earned 125 total points
Comment Utility
 //main purpose of this funtion is to return the number of donuts based on the amount of dozens that were given

 function parseDonuts(donutString) { // function name and variable parameters... called like .... $theDonuts = parseDonuts("3 dozen");
        numDonuts = parseInt(donutString);// parses the integer value of donutString
        if (donutString.indexOf("dozen") != -1) // checks to see if dozen is in the string.. if it is... then continue
          numDonuts *= 12; times the integer value from the string by 12 (a dozen)
        return numDonuts; // returns the number of donuts.. in the example in the first line the variable $theDonuts would hold this value
      }
0
 

Author Closing Comment

by:john8217
Comment Utility
Great. Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now