Newsgroup: it.comp.lang.javascript
Titolo: simple calculations

I'm extremely new to programming my own JavaScript and tried to create this program.  It asks for two numbers and then asks whether
you would like to multiply it by a random number.  If yes is typed,  it times' all three number.  If else,  times' first two numbers
only.  What is wrong with the code as an error is encountered on the var answer2.............. line.
<SCRIPT Language="JavaScript">
<!--
function add() {
var number1 = prompt("Please Enter A Number:");
var number2 = prompt("Please Enter Another Number:");
var random = prompt("Would You Like To Multiply By A Random Number (yes/no)");
if (random == yes)
var number3 = math.random();var answer1 = number1*number2*number3;window.alert(answer1);
  else {
var answer2 = number1*number2;window.alert(answer2);
}
}
//-->
</SCRIPT>
Also, any good debuggers other than Netscapes own for NN4.
And finally,  when I try something like
<SCRIPT Language="JavaScript">
<!--
function add() {
var number1 = prompt("Please Enter A Number:");
var number2 = prompt("Please Enter Another Number:");
var answer = number1+number2;
window.alert(answer);
}
//-->
</SCRIPT>
and enter number1 as 1 and number2 as 2,  does 12 appear rather than 3.
Thanks in advance
Ade
___________________________________________
Website: 
http://freespace.virgin.net/cm.parker/
AOL Instant Messenger: ade681299     Zone: MAParker
___________________________________________

...
:I'm extremely new to programming my own JavaScript and tried to create this
program.  It asks for two numbers and then asks whether
:you would like to multiply it by a random number.  If yes is typed,  it
times' all three number.  If else,  times' first two numbers
:only.  What is wrong with the code as an error is encountered on the var
answer2.............. line.
There are a few problems with your code, pointed out below, followed by an
enhanced working version:
:<SCRIPT Language="JavaScript">
:<!--
:function add() {
:var number1 = prompt("Please Enter A Number:");
:var number2 = prompt("Please Enter Another Number:");
:var random = prompt("Would You Like To Multiply By A Random Number
(yes/no)");
:
:if (random == yes)
---------------^^^
should be 'yes'
:var number3 = math.random();var answer1 =
number1*number2*number3;window.alert(answer1);
---------------^^^^
should be Math
also, your entire if clause needs to be wrapped in curly braces, as you are
wanting to execute multiple statements.
:
:  else {
:var answer2 = number1*number2;window.alert(answer2);
:}
:
:}
:
://-->
:</SCRIPT>
Here's a functional code block, with following mods:
* set default values for prompts
* convert the value of random to lowercase
* statements on their own lines, indentation changed to improve readability
<SCRIPT LANGUAGE="javascript"><!--
var number1 = prompt("Please Enter A Number:", "");
var number2 = prompt("Please Enter Another Number:", "");
var random = prompt("Would You Like To Multiply By A Random Number
(yes/no)", "no");
if (random.toLowerCase() == 'yes')
{ 
  var number3 = Math.random();
  var answer1 = number1 * number2 * number3;
  window.alert(answer1);
}
else 
{
  var answer2 = number1 * number2;
  window.alert(answer2);
}
</stuart>
<request>
 <item>no email copies of posts to clj</item>
 <item>no js questions via email; post to clj</item>
</request>
<reminder>
 java != javascript
</reminder>
//--></SCRIPT>