Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Javascript function question

Posted on 2010-08-30
Medium Priority
331 Views
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
Question by:john8217
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 18

Accepted Solution

Cluskitt earned 500 total points
ID: 33559162
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

Tiller79188231 earned 500 total points
ID: 33559230
//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

ID: 33580789
Great. Thank you.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to dynamically set the form action using jQuery.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
###### Suggested Courses
Course of the Month11 days, 13 hours left to enroll