Please help the question appears below and my so-called attempt appears after that as you can see I am relatively new to java, I only manage to do the sum part.

Write a script that takes three integers from the user and displays the sum. average, product, smallest and largest of the numbers in an alert dialog.

<script type="text/javajscript">
<!--	
var firstNumber, 
    secondNumber, 
    number 1,
    number2,
    number3,
    sum;	
firstNumber= window.prompt( 'Enter the first number", "0" );

secondNumber = window.prompt( 'Enter the second number", '0' );
ThirdNumber  = window.prompt( 'Enter the third number", '0' );
number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
sum = number1 + number2 +number3;
window.status =( sum: " + sum + "); 
<--
</script>
</head>
 <body onload="window.prompt;">

 </body>
</html>

When I run the above noting happens, the screen is absolutely blank and there’s no errors. This is for an assignment.

Java != JavaScript. They have similar names (purposefully done so by the JavaScript creators), and a somewhat similar syntax (again, purposefully done), but that's all. Next time post to a JavaScript forum. I have already requested that the admins move this one, so don't bother creating a new one there.

Please help the question appears below and my so-called attempt appears after that as you can see I am relatively new to java, I only manage to do the sum part.

Write a script that takes three integers from the user and displays the sum. average, product, smallest and largest of the numbers in an alert dialog.


<script type="text/javajscript">
<!--

/*
var firstNumber,
secondNumber, //this, and the following will produce errors
number 1,
number2,
number3,
sum;
*/
//you should declare your vars in one line when grouping:
var firstNumber, secondNumber, number 1, number2, number3, sum;

firstNumber= window.prompt( 'Enter the first number", "0" );

secondNumber = window.prompt( 'Enter the second number", '0' );
ThirdNumber = window.prompt( 'Enter the third number", '0' );
number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
sum = number1 + number2 +number3;
/*
window.status =( sum: " + sum + "); //this is where your code should fail completely!
*/

window.status = "sum: " + sum;
/*<--*/
-->
</script>
</head>
<body onload="window.prompt;">

</body>
</html>

When I run the above noting happens, the screen is absolutely blank and there’s no errors. This is for an assignment.

Your question is JavaScript related all right, but your code contains some errors. Whatch for portions marked in red.

Your question is JavaScript related all right, but your code contains some errors. Whatch for portions marked in red.

Thanks Troy III, for taking the time out to look at my script.

I’ve made the recommended changes but the problem still persists as it doesn’t prompt the user for the integer an I have no idea how to displays the average, product, smallest and largest of the numbers in an alert dialog. All I manage was the sum part.

<script type="text/javajscript">
<!--	

var firstNumber, secondNumber, number 1, number2, number3, sum;

firstNumber= window.prompt( 'Enter the first number", "0" );
secondNumber = window.prompt( 'Enter the second number", '0' );
ThirdNumber  = window.prompt( 'Enter the third number", '0' );
number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
sum = number1 + number2 +number3;

window.status = "sum: " + sum;

-->
</script>
</head>
 <body onload="window.prompt;">

 </body>
</html>

Thanks Troy III, for taking the time out to look at my script.

I’ve made the recommended changes but the problem still persists as it doesn’t prompt the user for the integer an I have no idea how to displays the average, product, smallest and largest of the numbers in an alert dialog. All I manage was the sum part.

<script type="text/javajscript">
<!--

var firstNumber, secondNumber, number 1, number2, number3, sum;

firstNumber= window.prompt( 'Enter the first number", "0" );
secondNumber = window.prompt( 'Enter the second number", '0' );
ThirdNumber = window.prompt( 'Enter the third number", '0' );
number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
sum = number1 + number2 +number3;

window.status = "sum: " + sum;

-->
</script>
</head>
<body onload="window.prompt;">

</body>
</html>

Here are some more changes.
1. script type="text/javascript". You have a typo.
2. its parseInt and not parselnt.
3.

<body onload="window.prompt;">

Doesn't make any sense to me. You don't need it.
And ofcourse, don't forget points mentioned by Troy III !

... All I manage was the sum part...

As a matter of a fact - your code will give you absolutely NOTHING!

Let me first explain you few things,
-now that you've learned that Java and JavaScript are two completely different languages, there is no single line in your code where you didn't put at least one [?deliberate!] error.


vars can be declared either separately:

var one;
var two;
var three;

or grouped with one keyword in a comma-separated list:

var one, two, three;

This will produce another error:

var one, tw o, three;

there is no comma between "tw" & "o", or there is an illegal whitespace in the name of var.
(your number 1 error reproduced).

You've commented your script <!-- ... <-- , this can never happen to be one of a "new to Java" error.

You have deliberately missmathched quotes window.prompt( 'Enter the first number", "0" ); in every single line required.

You've managed to switch "I" with "L" in every parselnt( firstNumber ); function counting on similar visual representation of I and smallcaps L in fonts currently used.

Finishing with your special mess:

window.status =( sum: " + sum + ");

using undefined sum: but converting the existing "sum" var into a string literal with no meaning at all.

Not to forget your prime error <script type="text/javajscript"> requiring the browser to use an unknown and inexistent scripting language to interpret your javascript. The most fundamental of all errors previously mentioned.

To round it up, your <body onload="window.prompt;"> is absolute nonesense.

I have no idea how to displays the average, product, smallest and largest of the numbers in an alert dialog.

That's because you didn't write anything of that kind in your (provided) code!

As a matter of a fact - your code will give you absolutely NOTHING!

Let me first explain you few things,
-now that you've learned that Java and JavaScript are two completely different languages, there is no single line in your code where you didn't put at least one [?deliberate!] error.


vars can be declared either separately:

var one;
var two;
var three;

or grouped with one keyword in a comma-separated list:

var one, two, three;

This will produce another error:

var one, tw o, three;

there is no comma between "tw" & "o", or there is an illegal whitespace in the name of var.
(your number 1 error reproduced).

You've commented your script <!-- ... <-- , this can never happen to be one of a "new to Java" error.

You have deliberately missmathched quotes window.prompt( 'Enter the first number", "0" ); in every single line required.

You've managed to switch "I" with "L" in every parselnt( firstNumber ); function counting on similar visual representation of I and smallcaps L in fonts currently used.

Finishing with your special mess:

window.status =( sum: " + sum + ");

using undefined sum: but converting the existing "sum" var into a string literal with no meaning at all.

Not to forget your prime error <script type="text/javajscript"> requiring the browser to use an unknown and inexistent scripting language to interpret your javascript. The most fundamental of all errors previously mentioned.

To round it up, your <body onload="window.prompt;"> is absolute nonesense.

That's because you didn't write anything of that kind in your (provided) code!

Ok this is what I have so far! You will agree its better that what I had when I first posted:

<script LANGUAGE="JAVASCRIPT">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0);
var thirdnumber = prompt("Enter third number and Click Ok", "0);
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
alert("The Sum is: " + total ) // this line give me the correct sum! But I need average, product, smallest and largest of the numbers as well.

alert("The Sum is: " + total + " Average: " + average + " product: " + product +" largest " + largest + " smallest " + smallest) //when I try to replace the above line with this one I get errors.

function YourCost1(){
if(firstnumber > secondnumber && firstnumber > thirdnumber){
var largest= firstnumber;
}
else if(secondnumber > firstnumber && secondnumber > thirdnumber){
var largest= secondnumber;
}
else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
var largest= thirdnumber;
}
if(firstnumber < secondnumber && firstnumber < thirdnumber){
var smallest= firstnumber;
}
else if(secondnumber < firstnumber && secondnumber < thirdnumber){
var smallest= secondnumber;
}
else if(thirdnumber < firstnumber && thirdnumber < secondnumber){
var smallest= thirdnumber;
}
}
</SCRIPT>

"average" has to do with, do it yourself math: ( a + b + c ) / 3, as in primary school.

sunny, you are again deliberately messing with quotes
("Enter second number and Click Ok", "0

you don't need it especially if you haven't writen O instead of 0!

your elseif
is one hell of a junk, there is only one elseif in both real life and in coding.

and there can't be the largest or the smallest "product" you mean "sum" of 1+2+3; -what on earth were you thinikng? the smallest and the largest is always the exact same value of 6,
etc etc ...

"average" has to do with, do it yourself math: ( a + b + c ) / 3, as in primary school.

sunny, you are again deliberately messing with quotes
("Enter second number and Click Ok", "0

you don't need it especially if you haven't writen O instead of 0!

your elseif
is one hell of a junk, there is only one elseif in both real life and in coding.

and there can't be the largest or the smallest "product" you mean "sum" of 1+2+3; -what on earth were you thinikng? the smallest and the largest is always the exact same value of 6,
etc etc ...

Ok I sort of worked it out. I still don’t know how to do the Product bit, Largest and Smallest. O and one more problem I cannot seem to get the sum and average onto the same alert box. I had to add two “alert” to check if the math is correct.

<script LANGUAGE="JAVASCRIPT">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var  total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var  average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
alert("The Sum is:" + total);
alert("average: "+average)


function YourCost1(){
  if(firstnumber > secondnumber && firstnumber > thirdnumber){
    var largest= firstnumber;
  
  }
  else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
    var largest= thirdnumber;
  }
  if(firstnumber < secondnumber && firstnumber < thirdnumber){
    var smallest= firstnumber;
  }
  else if(secondnumber < firstnumber && secondnumber < thirdnumber){
    var smallest= secondnumber;
  
  }
}
</SCRIPT>

OK
try: alert("The Sum is:" + total + "; The average is: " + average); , to alert them both in one turn.

try:

var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);

alert("smallest:" + smallest + "; largest: " + largest )

to remove the junk you use for this simple task: meaning following:

if(firstnumber > secondnumber && firstnumber > thirdnumber){
var largest= firstnumber;

}
else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
var largest= thirdnumber;
}
if(firstnumber < secondnumber && firstnumber < thirdnumber){
var smallest= firstnumber;
}
else if(secondnumber < firstnumber && secondnumber < thirdnumber){
var smallest= secondnumber;

taht can't posibly be working!

OK
try: alert("The Sum is:" + total + "; The average is: " + average); , to alert them both in one turn.

try:

var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);

alert("smallest:" + smallest + "; largest: " + largest )

to remove the junk you use for this simple task: meaning following:

if(firstnumber > secondnumber && firstnumber > thirdnumber){
var largest= firstnumber;

}
else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
var largest= thirdnumber;
}
if(firstnumber < secondnumber && firstnumber < thirdnumber){
var smallest= firstnumber;
}
else if(secondnumber < firstnumber && secondnumber < thirdnumber){
var smallest= secondnumber;

taht can't posibly be working!

Ok! I am almost there. I successfully manage to add the product bit. There seems to be a problem with the smallest bit I have to enter the follwing numbers in this order 89;44;20 a
This outputs 44 as the smallest if I change the order 20;44;89 then it outputs 20.

And most importantly all the outputs have to be one under the other I tried adding <br> and <p> tags but this dose not work.

And thanks for not giving up on me!

<script LANGUAGE="JAVASCRIPT">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum:" + total +"Average: " + average+ "; Product: " + product+ ";largest: " + largest + "; smallest:" + smallest)
</SCRIPT>

There seems to be a problem with the smallest bit I have to enter the follwing numbers in this order 89;44;20 a
This outputs 44 as the smallest if I change the order 20;44;89 then it outputs 20.

Are you sure about that ? I don't think so. It works fine for me.

And most importantly all the outputs have to be one under the other I tried adding <br> and <p> tags but this dose not work.

Try "\n".

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average + "\n smallest:" + smallest + "\n largest: " + largest +"\n")
</script> 
</head>
<body>
</body>
</html>

Are you sure about that ? I don't think so. It works fine for me.

Try "\n".

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average + "\n smallest:" + smallest + "\n largest: " + largest +"\n")
</script> 
</head>
<body>
</body>
</html>

Check this out when I run the script on ie5 it outputs 44 as the smallest but when I run it on fire fox the output is correct!

Sadly I can not add the print screen on the post as when I click the image link it ask for a url

I tested with IE6 and FF3 and I don't see any difference in the way they work ! I don't have IE5, so I can't really tell where its going wrong(?!?)

Check this out when I run the script on ie5 it outputs 44 as the smallest but when I run it on fire fox the output is correct!

Sadly I can not add the print screen on the post as when I click the image link it ask for a url

That's impossible, becasuse "Math.min" and "Math.max" are implemented in javascript version 1.0 and the IE5 uses, at least JS v1.1 engine.

You have introduced some other error in your code or input. alert(Math.min(120,845,12,8744,1,10,0,-278)) will return -278 (correct since it's the smallest number in the sequence) in all of the browsers supporting this object, otherwise you'll get an error, but never 0, nor 1 or 10 or some other wrong value!

Why don't you send us the code you are currently using to see if we can reproduce your wrong result?

That's impossible, becasuse "Math.min" and "Math.max" are implemented in javascript version 1.0 and the IE5 uses, at least JS v1.1 engine.

You have introduced some other error in your code or input. alert(Math.min(120,845,12,8744,1,10,0,-278)) will return -278 (correct since it's the smallest number in the sequence) in all of the browsers supporting this object, otherwise you'll get an error, but never 0, nor 1 or 10 or some other wrong value!

Why don't you send us the code you are currently using to see if we can reproduce your wrong result?

Hi there,

My completed code appears below, is there a way for me to submit a screen dump so you can see what I am talking about?

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average +"\n Product: " + product + "\n largest: " + largest +"\n smallest:" + smallest +"\n")
</script>
</head>
<body>
</body>
</html>

Hi there,

My completed code appears below, is there a way for me to submit a screen dump so you can see what I am talking about?

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average +"\n Product: " + product + "\n largest: " + largest +"\n smallest:" + smallest +"\n")
</script>
</head>
<body>
</body>
</html>

Yes there is, -there are plenty of image file hosters on the web, where you can upload your image for free [but please save it in a gif or jpg format, -don't link your reply with bmp or tif images if possible ].

Your current code should be working fine by the way.

I will soon post my code.

Well, now that you've learned that quotes can be used only in the logic of parenthesis (*) , but not (*] same as "*' etc, and that strings can be added with the + sign and automatically converted into stings when you ad some var value to it.

I also think that you've learned something that 99% of very old javascript coders don't know about Math.min and Math.max and similar, that is - you are not restricted to Math.min(x,y) or Math.max(x,y) only, as scripting reference examples describe it, since there is Math.min(x,y,z,...) .

Please, don't take what you read in books so literally.
Practice! And don't forget that JavaScript is very powerful language...

One thing I don't understand, is why is there no Average Math Object, in Math object collection. Something like Math.average(45,42,47,41,46) , that will return the average numeric value of the group, but that's not so interesting for discussion.

Therefore I've already made one, and introduced it in this example as a Troy III Script to JavaScript Math.object Extension, so that others who might need that functionality may use it also.

So here it is - your last working code rewritten:

<html>
<head>
<title>Implementing Math.average Extension</TITLE>

<script src="Troy III MathAverage.js" type="text/javascript"></script>

<script type="text/javascript">

var stNumber = parseFloat(prompt("Enter 1st number and Click OK", 0));
var ndNumber = parseFloat(prompt("Enter 2nd number and Click OK", 0));
var rdNumber = parseFloat(prompt("Enter 3rd number and Click OK", 0));

var total    = stNumber + ndNumber +  rdNumber;
var smallest = Math.min( stNumber, ndNumber, rdNumber );
var average  = Math.average( stNumber, ndNumber, rdNumber );
var largest  = Math.max( stNumber, ndNumber, rdNumber );
var product  = stNumber * ndNumber * rdNumber;

//rounding to 2 digit floating precision manually:

	total   = Math.round(total*100)/100;
	smallest= Math.round(smallest*100)/100;
	average = Math.round(average*100)/100;
	largest = Math.round(largest*100)/100;
	product = Math.round(product*100)/100;

alert(	   "Smallest: " + smallest+
	 "\n Average: "  + average +
	  "\n Largest: "  + largest +
	   "\n Sum: "      + total   + 
	    "\n Product: "  + product  )
</script>
</head>
<body>
<script type="text/javascript">
//Testing my Math.average extension with a really long sequence of numbers:

alert(	"The average number of the test sequence is: "+
	Math.average(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)
	)

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

The Math.average extension file attached below[418bytes].
My kind Regards 2all.

commented: Good work! +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.