Forum Moderators: open
<SCRIPT
language="JavaScript"
type="text/javascript">
/* Program to summarise the results of the election for Secretary.
There were 5 candidates.
*/
// Declare variables
var totalMembership, numberOfCandidates, totalVote;
var candidateNameArray;
var candidateVoteArray;
window.prompt('please enter the number of candidates','')
var candidateNameArray =('bloggs','bennett','dehesh','kelly','turner');
var candidateNameArray = new Array(5);
window.prompt('please enter the name of first candidate','')
window.prompt('please enter the name of the second candidate','')
window.prompt('please enter the name of the third candidate','')
window.prompt('please enter the name of the fourth candidate','')
window.prompt('please enter the name of the fifth candidate','')
// For each candidate the number of votes cast is entered
// and the running total of votes cast so far is calculated
window.prompt('please enter the number of votes cast for bloggs','')
window.prompt('please enter the number of votes cast for bennett','')
window.prompt('please enter the number of votes cast for dehesh','')
window.prompt('please enter the number of votes cast for kelly','')
window.prompt('please enter the number of votes cast for turner','')
numberOfCandidates = candidateNameArray.length;
numberOfVotes = candidateVoteArray.length;
totalVote = 0;
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
candidateVoteArray[count] = candidateVoteArray[count] +
parseFloat(window.prompt
('Please enter the votes cast for candidate' + (count + 1)));
totalVote = totalVote + candidateVoteArray[count];
}
// The total votes cast is written out
document.write('Total votes cast: ' + totalVote + '<BR>');
/*For each candidate, their total vote and their percentage share of the vote is output*/
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
var percentageVote;
percentageVote = (candidateVoteArray[count] / totalVote) * 100;
percentageVote = Math.round(percentageVote * 100) / 100;
document.write(candidateNameArray[count] +
'........votes: ' + candidateVoteArray[count] +
'........% of total vote: ' + percentageVote + '<BR>');
}
/* The winning candidate is declared by determining the array index with the largest vote
(assumes that there is an overall winner and no possibility of a tied ballot) */
var maxIndex;
maxIndex = 0;
for (var count = 0; count < candidateNameArray.length; count = count + 1)
if (candidateVoteArray[count] > candidateVoteArray[maxIndex])
{
maxIndex = count;
}
document.write('Winner is candidate' + (maxIndex+1) + ' with '
+ candidateVoteArray[maxIndex] + ' votes');
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
<html>
<head></head>
<body>
<script type="text/javascript">
// Declare candidates
var num = window.prompt('please enter the number of candidates','');
var candidates = new Array(num);
for (var count = 0; count < num ; count = count + 1) {
candidates[count] = window.prompt('please enter the name of candidate ' + (count + 1),'');
}
//input votes
var votes = new Array(num);
totalvotes = 0;
for (var count = 0; count < num ; count = count + 1) {
votes[count] = window.prompt('Please enter the number of votes for ' + candidates[count],'');
totalvotes = totalvotes + parseInt(votes[count]);
}
//display total votes
document.write('Total votes cast: ' + totalvotes + '<br />');
//For each candidate, their total vote and their percentage share of the vote is output
for (var count = 0; count < num ; count = count + 1) {
var percentageVote;
percentageVote = (votes[count] / totalvotes) * 100;
percentageVote = Math.round(percentageVote);
document.write(candidates[count] +
'- Votes: ' + votes[count] +
' - ' + percentageVote + '%<br />');
}
//compute winner
highest = 0;
for (var count = 1; count < num; count = count + 1) {
if (votes[count] > votes[highest]) {
highest = count;
}
}
document.write('Winner is candidate '+ candidates[highest] + ' with '
+ votes[highest] + ' votes');
</script>
</body>
</html>
Do you know how to amend the program you have wriiten so that it keeps track of the position of the largest vote as the votes for each candidate are entered. Thankyou...
<html>
<head></head>
<body>
<script type="text/javascript">
// Declare candidates
var num = window.prompt('please enter the number of candidates','');
var candidates = new Array(num);
for (var count = 0; count < num ; count = count + 1) {
candidates[count] = window.prompt('please enter the name of candidate ' + (count + 1),'');
}
//input votes
var votes = new Array(num);
totalvotes = 0;
for (var count = 0; count < num ; count = count + 1) {
votes[count] = window.prompt('Please enter the number of votes for ' + candidates[count],'');
totalvotes = totalvotes + parseInt(votes[count]);
}
//display total votes
document.write('Total votes cast: ' + totalvotes + '<br />');
//For each candidate, their total vote and their percentage share of the vote is output
for (var count = 0; count < num ; count = count + 1) {
var percentageVote;
percentageVote = (votes[count] / totalvotes) * 100;
percentageVote = Math.round(percentageVote);
document.write(candidates[count] +
'- Votes: ' + votes[count] +
' - ' + percentageVote + '%<br />');
}
//compute winner
highest = 0;
for (var count = 1; count < num; count = count + 1) {
if (votes[count] > votes[highest]) {
highest = count;
}
}
document.write('Winner is candidate '+ candidates[highest] + ' with '
+ votes[highest] + ' votes');
</script>
</body>
</html>
i take it you guys are on the M150 OU course
i am still suck on the fibonacci sequence one though any ideas
i tried the Fibonnaci one, i run it in firefox and it did not come up with any errors in the error console, but i still got no output. Anyways, i just sent it away like that, maybe i get some marks for trying.
This was my code i sent in.
<html>
<head>
</head>
<body>
<script Language="JavaScript">
function f()
{
var f=[];
}
function return(n)
{var f=[];
var fN;
var i = 20;
n = 2
f[0]=1;
f[1]=1;
if(i == 0)
return(n);
if(i == 1)
{
n = n + 1;
return(n);
}
else
{
for(fN=2;fN<=i;fN++)
{
f[fN]=f[fN-1]+f[fN-2];
document.write('f[fN]','');
}
f();
return(n);
}
}
</script>
<head>
</head>
</body>
</html>