User Name Password Register
DaniWeb IT Discussion Community
All
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 427,937 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 2,926 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: 1182 | Replies: 17
Reply
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Lost by tried and tried

  #1  
Jul 22nd, 2008
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.


  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 2:32 pm. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,461
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Lost by tried and tried

  #2  
Jul 22nd, 2008
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
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #3  
Jul 22nd, 2008
Originally Posted by stan.joe1 View 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.


<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.
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #4  
Jul 23rd, 2008
Originally Posted by Troy III View Post
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
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Lost by tried and tried

  #5  
Jul 23rd, 2008
Originally Posted by stan.joe1 View Post
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 !
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*
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #6  
Jul 23rd, 2008
Originally Posted by stan.joe1 View Post
... 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.

Originally Posted by stan.joe1 View Post
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!
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #7  
Jul 23rd, 2008
Originally Posted by Troy III View Post
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>
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #8  
Jul 23rd, 2008
"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 ...
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #9  
Jul 23rd, 2008
Originally Posted by Troy III View Post
"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
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #10  
Jul 23rd, 2008
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 9:53 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 6:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC