943,522 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 22nd, 2008
0

Lost by tried and tried

Expand Post »
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.


html Syntax (Toggle Plain Text)
  1. <script type="text/javajscript">
  2. <!--
  3. var firstNumber,
  4.   secondNumber,
  5.   number 1,
  6.   number2,
  7.   number3,
  8.   sum;
  9. firstNumber= window.prompt( 'Enter the first number", "0" );
  10.  
  11. secondNumber = window.prompt( 'Enter the second number", '0' );
  12. ThirdNumber = window.prompt( 'Enter the third number", '0' );
  13. number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
  14. sum = number1 + number2 +number3;
  15. window.status =( sum: " + sum + ");
  16. <--
  17. </script>
  18. </head>
  19. <body onload="window.prompt;">
  20.  
  21. </body>
  22. </html>

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



Last edited by peter_budo; Jul 22nd, 2008 at 3:32 pm. Reason: Keep It Organized - please use [code] tags
Similar Threads
Reputation Points: 7
Solved Threads: 0
Newbie Poster
stan.joe1 is offline Offline
12 posts
since Apr 2008
Jul 22nd, 2008
0

Re: Lost by tried and tried

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 22nd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by stan.joe1 ...
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.
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Jul 23rd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by Troy III ...
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javajscript">
  2. <!--
  3.  
  4. var firstNumber, secondNumber, number 1, number2, number3, sum;
  5.  
  6. firstNumber= window.prompt( 'Enter the first number", "0" );
  7. secondNumber = window.prompt( 'Enter the second number", '0' );
  8. ThirdNumber = window.prompt( 'Enter the third number", '0' );
  9. number1 = parselnt( firstNumber ); number2 = parselnt( secondNumber );number3 = parselnt( ThirdNumber );
  10. sum = number1 + number2 +number3;
  11.  
  12. window.status = "sum: " + sum;
  13.  
  14. -->
  15. </script>
  16. </head>
  17. <body onload="window.prompt;">
  18.  
  19. </body>
  20. </html>
Last edited by Tekmaven; Jul 24th, 2008 at 3:14 am. Reason: Code tags
Reputation Points: 7
Solved Threads: 0
Newbie Poster
stan.joe1 is offline Offline
12 posts
since Apr 2008
Jul 23rd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by stan.joe1 ...
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.
Quote ...
<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 !
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 23rd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by stan.joe1 ...
... 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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var one;
  2. var two;
  3. var three;
or grouped with one keyword in a comma-separated list:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var one, two, three;
This will produce another error:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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.

Click to Expand / Collapse  Quote originally posted by stan.joe1 ...
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!
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Jul 23rd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by Troy III ...
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var one;
  2. var two;
  3. var three;
or grouped with one keyword in a comma-separated list:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var one, two, three;
This will produce another error:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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>
Reputation Points: 7
Solved Threads: 0
Newbie Poster
stan.joe1 is offline Offline
12 posts
since Apr 2008
Jul 23rd, 2008
0

Re: Lost by tried and tried

"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 ...
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Jul 23rd, 2008
0

Re: Lost by tried and tried

Click to Expand / Collapse  Quote originally posted by Troy III ...
"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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script LANGUAGE="JAVASCRIPT">
  2. var firstnumber = prompt("Enter first number and Click OK", "0");
  3. var secondnumber = prompt("Enter second number and Click Ok", "0");
  4. var thirdnumber = prompt("Enter third number and Click Ok", "0");
  5. var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
  6. var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
  7. alert("The Sum is:" + total);
  8. alert("average: "+average)
  9.  
  10.  
  11. function YourCost1(){
  12. if(firstnumber > secondnumber && firstnumber > thirdnumber){
  13. var largest= firstnumber;
  14.  
  15. }
  16. else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
  17. var largest= thirdnumber;
  18. }
  19. if(firstnumber < secondnumber && firstnumber < thirdnumber){
  20. var smallest= firstnumber;
  21. }
  22. else if(secondnumber < firstnumber && secondnumber < thirdnumber){
  23. var smallest= secondnumber;
  24.  
  25. }
  26. }
  27. </SCRIPT>
Last edited by Tekmaven; Jul 24th, 2008 at 3:20 am. Reason: Code tags
Reputation Points: 7
Solved Threads: 0
Newbie Poster
stan.joe1 is offline Offline
12 posts
since Apr 2008
Jul 23rd, 2008
1

Re: Lost by tried and tried

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!
Last edited by Troy III; Jul 23rd, 2008 at 10:53 am.
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: urgent help needed pls..............
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Is this Strange problem due to virus ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC