I need help to do below task. Would anybody help? Create a ColdFusion component(.cfc) that contains the following functions: Function: calculateTriangleArea Description: Returns the area of a triangle based on the specified parameters Input Variables: base (Numeric), height (Numeric) Returns: Numeric Additional Details: If any of the input variables are equivalent to zero or are less than zero, the function should return 0. Function: findFourLetterWords Description: Returns a list of four letter words based on a list of entries Input Variables: wordList (String) Returns: String Additional Details: wordList is a string variable containing a list of one or more words. The function reviews each word and then returns a list of words from the original list that are four letters. Crate index.cfm file, demonstrating your ColdFusion component as follows, Invoke the calculateTriangleArea function three times with the following inputs: Base = 5, Height = 4 Base = 0, Height = 10 Base = 9, Height = 0 If the area of the triangle is greater than 0, display the following message, filling in the blanks: "The area of a triangle with base: ___ and height: ____ is: ____". If the area of the triangle is not greater than 0, display the following message. "Error. Could not calculate area with base: ___ and height: ___." Invoke the findFourLetterWords function two times with the following inputs: wordList = apple, pear, banana, grape, lime wordList = Maryland, Virginia, North Carolina, Florida If the list contains at least one four letter word, display the following message, filling in the blanks: "The list: ___ contains the following four letter words: ___". If there are no four letter words in the list, display the following message. "There are no four letter words in the list: ___." ++++++++++++++++++ This is what i have so far; <cfcomponent> <cffunction name="calculateTriangleArea" access="public" returntype="numeric"> <cfargument name="area" type="numeric" required="yes"> <cfset var result=StructNew()> <cfset result.area=area> <cfset result.area=(1/2) * base * height> <cfreturn result> </cffunction> <cffunction name="findFourLetterWords" access="public" returntype="string"> <cfargument name="wordList" type="string" required="yes"> <cfset var list=StructNew()> <cfset result.wordList=worldList> <cfreturn list> </cffunction> </cfcomponent> +++++++++++++++++++++++++ Many Thanks
What part are you having problems with? While I won't do your homework for you, if you have a specific question I'd be happy to answer it if I can.
I just want to know what am i doing wrong in the below code? And also how should I call this with in the .cfm and how to invoke it? I need some instructions to do the code. <cfcomponent> <cffunction name="calculateTriangleArea" access="public" returntype="numeric"> <cfargument name="area" type="numeric" required="yes"> <cfset var result=StructNew()> <cfset result.area=area> <cfset result.area=(1/2) * base * height> <cfreturn result> </cffunction> <cffunction name="findFourLetterWords" access="public" returntype="string"> <cfargument name="wordList" type="string" required="yes"> <cfset var list=StructNew()> <cfset result.wordList=worldList> <cfreturn list> </cffunction> </cfcomponent>
What do you mean by "wrong"? What is happening now and how is it different than what you want to happen? The code you posted is a CF component. It should be saved as a *.CFC file. The file name can be anything you want, as long as the extension is *.CFC. To invoke it create a separate *.cfm page and use CFINVOKE. http://livedocs.adobe.com/coldfusion/8/Tags_i_10.html Say you saved your component to file "c:\coldfusion8\wwwroot\MyComponent.cfc" and wanted to invoke the function "calculateTriangleArea". Using cfinvoke you provide - the component name ("MyComponent" - do NOT include the extension .cfc) - the function you want to call ("calculateTriangleArea") - any arguments required by that function. ("area") - returnVariable (name of the variable to hold the results) <cfinvoke name="MyComponent" method="calculateTriangleArea" area="Some value here" returnVariable="MyResult" > Code (markup): There's others ways to invoke it. But that's the most intuitive. BUT you'll probably get an error with your function. Take a look at the code. It isn't correct. You're got the wrong input variables (or arguments). They're supposed to be "base" and "height". But your function uses "area".
Thanks for the support- I think I got the first part correct. But having problems with the second part (wordList) How should I find from the list contains at least one four letter word? I created the componets part but I am not sure about the argument that needs in here. Any input about that. <cffunction name="findFourLetterWords" access="public" returntype="string" output="no" description="Returns a list of four letter words based on a list of entries" hint="wordList is a string variable containing a list of one or more words. The function reviews each word and then returns a list of words from the original list that are four letters. "> <cfargument name="wordList" type="string" required="yes"> <cfset var list=wordList> <cfreturn list> </cffunction> </cfcomponent>
So you fixed the cfarguments? That part is simple. The instructions already tell you exactly what arguments you need for both functions. Forgetting about CF code, how would *you* achieve it? ie Write down in plain english what basic steps would follow ...? That's what I do when I have a problem like this. Write it out in pseudo-code first. Then find the tags or functions that will make it work. http://livedocs.adobe.com/coldfusion/8/functions-pt0_02.html#3532194
That one always scares 'em away. lol. But how else do you write code? But create an outline of the steps you think you need .. then find what functions do those things. Test it. Improve it. Retest.
Ok- I am still working on this but my code does not do the word count from the string and output it correctly. The loop has an issue but i cannot fix it. Need help for that if you can. This is the .cfc <cffunction name="findFourLetterWords" access="public" returntype="string" output="no" > <cfargument name="wordList" type="string" required="yes"> <cfloop index = "list" list = "#wordList#"> <cfoutput>#list#</cfoutput> <cfset letters=Len(#list#)> <cfoutput> #letters# </cfoutput> </cfloop> <cfreturn list> </cffunction> This is the .cfm file <cfinvoke component="exercise1" method="findFourLetterWords" returnvariable="list"> <cfinvokeargument name="wordlist" value="apple,pear,banana,grape,lime"> </cfinvoke> <cfset wordList="apple,pear,banana,grape,lime"> <p> <cfoutput> <cfif list eq wordList> The list: #wordList# contains the following four letter words:#list#. <br /> <cfelse> There are no four letter words in the list: #wordList#. <br /> </cfif> </cfoutput>
Ok but "what" is the issue? What is the code doing now, and what *should* it be doing. At a guess I would say it's because you're not actually doing anything with the length of the string inside the function. If you want it to treat some of the elements differently, you have to code it. Write down the psuedo-steps in english first... then we can go from there.