Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 7th, 2008, 04:35 AM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default JavaScript-Plz save my soul.

Hi everyone,
Plz I am a student and I have tried everything to solve the following
problem below without results.Help plz:

Write a function called calcAverage(strData). The function receives one parameter strData ? which is a comma delimited string of data values. The function needs to compute the average of these numbers and then return that value. The function should also detect for invalid data ? i.e. non-numeric data. If an invalid data value is encountered the function should simply ignore that data value and continue processing.. Desk check your function using the expected input and output data below. Also keep in mind that your function needs to handle variable lengths of input data.


<html>

<head>

<script type="text/javascript">

function calcAverage(strData) {

// provide code and comments to implement this function.

}

</script>

</head>

<body>

----

<script type="text/javascript">

document.write(`The average of numbers ` + strData + ` is: `+calcAverage(strData));

</script>

</body>

</html>

Input: strData
 Output: dataAverage

?1,10,A,100?
 37

?2,4,6,8,10,12?
 7




  #2 (permalink)  
Old November 7th, 2008, 09:04 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello... What did you try??? this is a simple function, but this forum is for asp.net, maybe you want to try in the javascript forum???

anyway, if you wanna do it by yourself and learn a little, tell me what you do (or show me) and we can walk you to the correct function...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #3 (permalink)  
Old November 7th, 2008, 09:57 AM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Gonzalo,
Thanks.This is my strenght below:
<html>
<body>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
 function computePay(strData){
     var strData = (computePay);


  //for (var i = 0; i < computePay.length; i++) {
         {

            //if (strData != "") strData += "";
            strData += computePay.name("Sally");
            strData += computePay.hours("20 * 15.50");
            strData += computePay.name("Mary");
            strData += computePay.A("NaN");
            document.getElementsById("name +hours +name +A" );
        }
    }

 </script>
 </body>
 </html>
Thanks.
I pray you show me the way.Plz I still learning the trade.I am sorry.
Magxtopher


  #4 (permalink)  
Old November 7th, 2008, 10:52 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

I don't know much Javascript, but if computeday is the name of the function, how can it be also the array??? your array is strdata. so you are receiving the data!. You have to look for a function that can search for the first comma, then try to print every value between comma. use the for loop to do that..

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #5 (permalink)  
Old November 7th, 2008, 11:04 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. This function is a mess.

This makes no sense:
var strData = (computePay);
(Honestly, I am not sure how it did not throw you an error)

Anyway, just because you are passing in a comma deliminted string doesn't mean you can iterate through it like an array, it is just a string after all. You are looking for something like this (I think):

function ComputePay(data)
{
   var dataArray = data.split(",");
   for(i = 0; i < dataArray.length; i++)
   {
     if(!isNaN(dataArray[i]))
     {
        //do computation
     }
   }
}

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
  #6 (permalink)  
Old November 7th, 2008, 01:01 PM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Gonzalo and Doug,
When I reload the code nothing popup.Why?
Plz bail me out.Here is the code:
<html>
<body>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
 function ComputePay(data)
{
   var dataArray = data.split(",");
   for(i = 0; i < dataArray.length; i++)
   {
     if(!isNaN(dataArray[i]))
     {
      var dataArray = "Sally,20,15.50,Mary,32,A";
      var TextArray = Data.split("<br>");
      //do computation
        }
       document.write("myTextArray");
   }


}
 </script>
 </body>
 </html>

  #7 (permalink)  
Old November 7th, 2008, 01:49 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

1. You never call the javascript function thus it never runs.
2. What is this: var TextArray = Data.split("<br>");? It seems to make no sense.
3. Why are you trying to redeclare dataArray inside the for loop?

It is quite obvious that you do not understand JavaScript at all. I understand that you are a student but you might want to go back and study a little bit more because you are miscueing on even the most basic fundamentals. (In this case you are trying to declare 2 variables with the same name.)

hth.
-Doug
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
  #8 (permalink)  
Old November 8th, 2008, 01:30 AM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Doug,
Yes I want to study and already student.
As a teacher kindly teach me these one.
When you look at the previous code and now.
I remove the function part of it and whole
code work but not as I pray.
I use regular expression.
1)I still want the function part of the code.
2)Your second to the last code will result a long
code(even as a novice I think so).
3)I want short code with the following result:
   "Sally,310.00,Mary,NaN".
4)Listen I run the code without any change from the Input:
  <html>
<body>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
//function ComputePay(data){
  //{
var StrData = "Sally,20,15.50,Mary,32,A";
//
var theRegExp = /[^a-z] +[\d]/;
var strResults = StrData.split (theRegExp);

document.write(strResults.join ("<br>"));


</script>
</boby>
</html>
Thanks.

Magxtopher.

  #9 (permalink)  
Old November 8th, 2008, 03:16 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You are just throwing any code you can find at the problem without trying to understand what it does.

You say you have a JavaScript book, yes?

So start with it. Start with Chapter 1. Read it. DO THE EXERCISES in the book.

None of your code so far has made ANY SENSE AT ALL. None.

In this latest effort, you have this:
Code:
    var theRegExp = /[^a-z] +[\d]/;
Do you have ANY idea what that means???

It means "search for a string of this form:
    -- Any character *except* a lowercase letter.
    -- Followed by ONE OR MORE spaces
    -- Followed by one digit.

And then you try to use that expression to SPLIT this string:
Code:
    var StrData = "Sally,20,15.50,Mary,32,A";
But NO PLACE in that string is there ANY string of characters that match the regular expression! Because NO PLACE in that string is there a space, and you MUST have a space to match the expression!

Okay, let's get rid of the space. Let's change it to:
Code:
    var theRegExp = /[^a-z][\d]/;
So that says "any character EXCEPT a lower case letter that is then followed by a digit".

So look at the possible split points in that string:
Code:
    var StrData = "Sally[u],2</u>0[u],1</u>5[u].5</u>0,Mary[u],3</u>A";
Each of those [u]red underlined</u> pairs of characters matches the expression: "A character that is not a lower case letter and which is followed by a digit."

So when you finally SPLIT using that regular expression, you will have an ARRAY OF STRING with the following elements:
Code:
    Sally
    0
    5
    0,Mary
    2,A
Because the SPLIT *removes* the characters that you do the split ON.

Does that even come CLOSE to what you are after???

Of course not.

So STOP just coding. FIRST think about what you need to do. Then come up with a PLAN. Then try to CODE the plan.

It doesn't do any good to throw random code at a problem. You will never solve the problem.

More than half of the work in programming is *NOT* coding. Most of the work is (a) defining the problem, (b) designing an approach to the solution, (c) designing a way you can test that your solution is the right one. Only then do you start the coding. ONLY THEN.
  #10 (permalink)  
Old November 8th, 2008, 03:21 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'll tell you what. I'll give you a couple of HINTS.

But we do *NOT* do homework here. If we do your homework for you, how will you ever learn? Oh, you might learn and understand the code we give you. But you won't learn how to be a *programmer*. To be a programmer, you must THINK like a programmer. Not everyone is ready to do that.

Anyway, hints:

HINT 1: Since your teacher said the input is delimited by commas, then why would you want to use anything *except* a comma in the split( )????

HINT 2: To find out if a string is a valid number, use parseFloat( ) and isNaN( ). I will let you look those up in your JavaScript book to find out how to use them.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 02:54 AM
Excel Save Prompt in javascript interrupt Javascript How-To 2 March 18th, 2005 04:52 AM
not able to save data using javascript rok Classic ASP Databases 0 April 21st, 2004 04:31 AM
error using XML save method in javascript Heroic Dose Javascript 3 September 22nd, 2003 08:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.