/    Sign up×
Community /Pin to ProfileBookmark

recognizing double digit numbers

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>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 18.2010 — [CODE]numbers = t.split(/s[COLOR="Blue"]+[/COLOR]/);[/CODE]
Copy linkTweet thisAlerts:
@TuskauthorOct 20.2010 — IT works YAY!!!

Thank you Fang, you are a wizard sir ?
×

Success!

Help @Tusk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...