•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,018 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,711 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1165 | Replies: 17
![]() |
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Rep Power: 0
Solved Threads: 0
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.
When I run the above noting happens, the screen is absolutely blank and there’s no errors. This is for an assignment.
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)
<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.
Last edited by peter_budo : Jul 22nd, 2008 at 2:32 pm. Reason: Keep It Organized - please use [code] tags
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
•
•
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.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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>
Last edited by Tekmaven : Jul 24th, 2008 at 2:14 am. Reason: Code tags
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 240
•
•
•
•
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;">
And ofcourse, don't forget points mentioned by Troy III !
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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;
var one, two, three;
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 + ");
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!
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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:
or grouped with one keyword in a comma-separated list:var one; var two; var three;
This will produce another error:var one, two, three;
var one, tw o, three;
there is no comma between "tw" & "o", or there is an illegal whitespace in the name of var.
(yournumber 1error 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 everyparselnt( firstNumber );function counting on similar visual representation of I and smallcaps L in fonts currently used.
Finishing with your special mess:
using undefinedwindow.status =( sum: " + sum + ");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 ...
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 ...
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
"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> Last edited by Tekmaven : Jul 24th, 2008 at 2:20 am. Reason: Code tags
OK
try:
try: to remove the junk you use for this simple task: meaning following: taht can't posibly be working!
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 )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; Last edited by Troy III : Jul 23rd, 2008 at 9:53 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Body Text lost when sending hotmail (Web Browsers)
- Quick Launch toolbar lost (Windows 9x / Me)
- Lost browser links,internet options etc...toolbar (Web Browsers)
- Have I lost RAM? (Motherboards, CPUs and RAM)
- recover lost file in windows XP (Windows NT / 2000 / XP / 2003)
- lost address book addresses (OS X)
- IDE devices lost during post W2K (Windows NT / 2000 / XP / 2003)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: urgent help needed pls..............
- Next Thread: Is this Strange problem due to virus ?



Linear Mode