Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

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?



<!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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Massimo Scola

ASKER

Great - thanks for spotting this for me!
You're welcome :)