/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript Addition…

Hey guys, first want to say this board is great. Been lurking here, and alot of useful information to be had…

I am having a problem getting a simple addition problem setup. I am setting up a survey to rate how well a task was performed, between 0-100. There will be 25 questions, and each question will have a point value of 0-4. All I need the script to do, is calculate the total point value, from all 25 questions, once they have been answered. It doesnt matter if the point values are calculated live, or if you have to press a “calculate” button. Let me give you an exaple of how I have the form setup..

<tr>
<td width-“350”>Appeared confident
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body12″ value=”0″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body12″ value=”1″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body12″ value=”2″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body12″ value=”3″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body12″ value=”4″>
</td>
</tr>
<tr>
<td width-“350”>Sounded professional
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body13″ value=”0″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body13″ value=”1″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body13″ value=”2″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body13″ value=”3″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body13″ value=”4″>
</td>
</tr>
<tr>
<td width-“350”>Remained empathetic/caring
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body14″ value=”0″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body14″ value=”1″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body14″ value=”2″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body14″ value=”3″>
</td>
<td width=”65″ align=”center”><INPUT type=”radio” name=”body14″ value=”4″>
</td>
</tr>

Thanks for all those that read and replied. Im really greatful..

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieAug 30.2004 — <i>
</i>function calculate(){
var total;
total = 0;
for(var i=12; i&lt;15;i++){
var eleArr = document.getElementsByName('body'+i);
for(var j=0;j&lt;eleArr.length;j++){
if(eleArr[j].checked){
alert(eleArr[j].value);
total += Number(eleArr[j].value)
}
}
}
alert(total);
}

above is coded from your example provided. To make it work for yr form is going to need a few mods..
Copy linkTweet thisAlerts:
@low0rauthorAug 30.2004 — Thanks for the reply... Im a newb at this, so im not sure what I would need to edit... Here is what I have... I have 16 Body questions, and 9 Open questions... The open questions are yes or no questions. A yes is 2 points, a no is 0 points. The questions I have posted are rated from Not Acceptable to Exceptionable, and then their point value. If you perform as expected on this test, you will receive a 50.

So that is a little confusing, and I dont really understand it... Im sorry for being a PITA. thanks for your reply, i appreceiate it..
Copy linkTweet thisAlerts:
@javaNoobieAug 30.2004 — [b]I have 16 Body questions[/b][/quote]
assuming you have named them body0,body1,body2,..,body15 for each question.. modify the code to the following in the 'for' statement.
<i>
</i>for(var i=0; i&lt;=15;i++)

[b]and 9 Open questions...[/b][/quote]
assuming u did the same for the body question and naming the open questions open0,open1,open2,..,open8. use the same logic from the body part for this. NOTE: change to 'open' instead of 'body'.
[b]A yes is 2 points, a no is 0 points[/b][/quote]
this will have to be done in the html portion.
Copy linkTweet thisAlerts:
@low0rauthorAug 30.2004 — Yes, you assumed correctly. I have not had a chance to test this yet, however, I want to thank you for the time you have put into this. I do have the yes and no questions setup in the html already.

Once again, thank you so much for your time...
Copy linkTweet thisAlerts:
@low0rauthorAug 30.2004 — So you said I had to do the same thing for the Open questions, that I did for the body questions? What if I just rename them all the body, then had the numbers from 0-24? Would I change this line to:


for(var i=0; i<=24;i++){
Copy linkTweet thisAlerts:
@javaNoobieAug 31.2004 — Yes you are right.
Copy linkTweet thisAlerts:
@low0rauthorAug 31.2004 — It worked great!!! Thank you so much!!

I had to remove:

alert(eleArr[j].value);

Because it would pop up the answer for every question, so I had a score popping up 25 times, then the final score.. Once I figured that out, I was all great! Thanks!
×

Success!

Help @low0r 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...