/    Sign up×
Community /Pin to ProfileBookmark

can someone look at my javascript codes please?

I am supposed to write a program that prompts user to enter the numbers separated by space and when user click “ok” button, it should display the numbers entered with its mean, and standard deviation and the window should ask if user wants to enter numbers again if yes then it should display the new numbers entered, with its mean and standard deviation(old numbers with mean, variance and deviation should be replaced by the new one). the report method should use dynamic HTML so that each new report overlays the old one.

I created an external javascript file and embedded inside the strict XHTML file

I think I got all my functions…Can anyone look into my codes once again!! FOR SOME REASON IT IS NOT WORKING

[code] var n
function ListNumber(num)
{
this.num = n;
}
ListNumber.prototype.numberReader = numberReader();
ListNumber.prototype.meanCalculation = meanCalculation();
ListNumber.prototype.varianceCalculation = varianceCalculation();
ListNumber.prototype.stdDeviation = stdDeviation();
ListNumber.prototype.report = report();[/code]

[code]function numberReader()
{
var i;
this.n = prompt(“Enter a list of numbers separated by spaces:”, “”).split(” “);
for(i=0;i<this.n.length;i++)
{
document.write(“<br />[“+i+”] = “+this.n[i]);
}
}[/code]

[code]function meanCalculation()
{
var i;
var sum;
var mean;
for(int i = 0; i<this.n.length;i++)
{
sum+=n[i];
}
mean=sum/this.n.length;
}[/code]

[code]function varianceCalculation()
{
var variance;
var i;
var sum;
var meanValue;
meanValue = this.meanCalculation();
for(i = 0; i<this.n.length; i++)
{
sum[i]+= (this.n[i]-meanValue)*(this.n[i]-meanValue);
}
return (sum/(this.n.length -1));
}[/code]

[code]function stdDeviation()
{
var stdDeviation;
stdDeviation =Math.sqrt(this.varianceCalculation());
}[/code]

[code]function report()
{
var choice;
numberReader();
document.getElementById(“Number list”).innerHTML = reportContent;
document.write(“<p> mean:”+ this.meanCalculation()+”.</p>”);
document.write(“<p> variance:”+this.varianceCalculation()+”.<p”>);
document.write(“<p> standard deviation:”+ this.stdDeviation()+”.</p>”);
choice = confirm(“Do you want another calculation”)
if(choice)
{
numberReader();
meanCalculation()
(am I doing right thing?????)
}[/code]

Here is my html file :-

[code]
<html>
<head>
<title> random calc</title>
<script type = “text/javascript” src = “math.js”></script>
</head>
<body>
<p>using the prompt enter numbers</p>
<script type = “text/javascript”>
var ab;
var n;
ab = new ListNumber(n);
document.write(“<p>Numbers:”+ab.numberReader()+”</p>”);
document.write(“<p>mean:”+ab.meanCalculation()+”</p>”);
</script>
</body>
</html>
[/code]

Seems like I have done mistake somewhere, can anyone check my code please please!!
thanks,

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@voidvectorNov 23.2008 — 
  • 1. when you prototype functions, you don't put the parenthesis at the end.
    [CODE]ListNumber.prototype.numberReader = numberReader;
    [/CODE]


  • 2. turn on JavaScript debugger in IE, or open the error console on other browsers when you trying to test JavaScript. They will tell you syntax and runtime errors like using "int" to define variables and accessing unavailable variables.


  • 3. Don't use object-oriented JavaScript until you are more proficient with regular JavaScript.


  • Nevertheless, here's the working code: (I have no clue what you mean by overlay)

    [code=html]<html>
    <head>
    <title> random calc</title>
    <script type = "text/javascript" src = "math.js"></script>
    </head>
    <body>
    <p>using the prompt enter numbers</p>
    <script type = "text/javascript">
    var ab = new ListNumber();
    this.report();
    </script>
    </body>
    </html>
    [/code]


    [CODE]
    function ListNumber()
    {
    this.num;
    }

    ListNumber.prototype.numberReader = numberReader;
    ListNumber.prototype.meanCalculation = meanCalculation;
    ListNumber.prototype.varianceCalculation = varianceCalculation;
    ListNumber.prototype.stdDeviation = stdDeviation;
    ListNumber.prototype.report = report;

    function numberReader()
    {
    this.num = prompt("Enter a list of numbers separated by spaces:", "").split(" ");

    for (var i = 0; i < this.num.length; i++)
    this.num[i] = parseFloat(this.num[i]);
    }

    function meanCalculation()
    {
    var sum = 0;
    for(var i = 0; i < this.num.length; i++)
    {
    sum += this.num[i];
    }
    return sum / this.num.length;
    }

    function varianceCalculation()
    {
    var sum = 0;
    var meanValue = this.meanCalculation();
    for(var i = 0; i < this.num.length; i++)
    {
    sum += (this.num[i]-meanValue)*(this.num[i]-meanValue);
    }
    return (sum/(this.num.length - 1));
    }

    function stdDeviation()
    {
    return Math.sqrt(this.varianceCalculation());
    }

    function report()
    {
    var choice;
    this.numberReader();
    var buffer = '';
    for(var i = 0;i<this.num.length;i++)
    {
    buffer += "<br />["+i+"] = "+this.num[i];
    }
    buffer += "<p> mean: "+ this.meanCalculation() + ".</p>";
    buffer += "<p> variance: "+this.varianceCalculation() + ".</p>";
    buffer += "<p> standard deviation: "+ this.stdDeviation() + ".</p>";
    document.write (buffer);
    choice = confirm("Do you want another calculation");
    if(choice)
    {
    this.report();
    }
    }
    [/CODE]
    ×

    Success!

    Help @newbee_coder spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...