Help please I have encounted another problem in the result, for some reason my function is not recognizing double (or greater) digit numbers it converts them to single digits e.g “23 4 55” it converts to “2+3+4+5+5=19” and not “23+4+55=82”
[code]
<head>
<script type=”text/javascript” src=”https://getfirebug.com/firebug-lite-debug.js”></script>
<meta http-equiv=’Content-type’ content=’text/html;charset=UTF-8′>
<title>Calc Numbers</title>
<script type=’text/javascript’>
function calc() {
// calc() is designed to sum numbers entered in an input field
var t = document.getElementById(“num”).value; // the numbers entered
var numbers = new Array();
numbers = t.split(/s*/); // the numbers extracted from the string
var sum = 0; // variable to hold the sum of the numbers
var num = ”; // string to hold a list of the numbers
if (numbers.length == 1) { num = ‘0’; } // to add single number to
for (i = 0; i < numbers.length; i++) {
sum = sum + eval(numbers[i]); // add this number
if (i == numbers.length – 1) { num = num + ” and “;} // last number?
else { if (i > 0) { num = num + “, “} } // otherwise if not 1st, join with ,
num = num + numbers[i]; // and add the number to the list
}
// create the text to display with the result
result = “<p>The sum of the numbers ” + num + ” is ” + sum +”<“+”/p>”;
// find the placeholder for displaying the result
var resElement = document.getElementById(“res”);
// display the result
resElement.innerHTML = result;
}
</script>
</head>
<body>
<h1>Calc Numbers</h1>
<p>Enter some numbers separated by spaces:</p>
<textarea id=’num’ onclick=”this.value=”” onchange=’calc()’ cols = 65 rows = 5 wrap=”PHYSICAL”></textarea>
<!– provide a dummy button – encourage user to leave input tag –>
<input type=”button” value=”Calc”>
<!– the following div id=’res’ provides a placeholder
that calc() can use to display the result –>
<div id=’res’></div>
</body>