Massimo Scola
asked on
Basic JavaScript Question
This is a very basic JavaScript question:
I copied the following code from my JavaScript book into an HTML editor.
Does the code - the way it is written - not run because the function isn't called?
How do I make this code work?
I copied the following code from my JavaScript book into an HTML editor.
Does the code - the way it is written - not run because the function isn't called?
How do I make this code work?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
/* <![CDATA[ */
function addNumber(number1, number2) {
var number3 = parseInt(number1) + parseInt(number2);
//the next line returns the value of total back
//to the global context
//otherwise it would not be available any more
return number3;
}
/* ]]> */
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript Test</title>
</head>
<body>
<script type="text/javascript">
var total = 0;
var number = prompt("Add a list of numbers. Type a number or '.' to exit.""");
while(number!=".") {
total = addNumber(number,total);
number = prompt("Add a list of numbers. Type a number or '.' to exit.", "");
}
alert("The total is " + total);
</script>
<p>This script just shows how to use functions.</p>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome :)
ASKER