Forum Moderators: open

Message Too Old, No Replies

new to javascript and stuck

no output displayed from program

         

gunter

6:14 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



I am new to javascript, infact i have no real idea what i am doing. Could someone please be kind enough to fix the following script so it displays the output on the webpage instead of staying blank. Thankyou...

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

delboy1978uk

1:20 am on Jun 23, 2009 (gmt 0)

10+ Year Member



im new too, but i messed around and figured it out!
:-D <-- Well Proud!

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

gunter

4:27 am on Jun 23, 2009 (gmt 0)

10+ Year Member



Thankyou very much Delboy, this is seriously appreciated. I messed around for weeks and everytime i thought i had solved it, a different part would not work lol.

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

dizne

12:31 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



This already has the highest vote //compute winner

I take you are on a course. What one / where?

Cheers

alianze

9:30 am on Jun 26, 2009 (gmt 0)

10+ Year Member



Nice one i got this before we had to track the votes

<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

dizne

11:19 am on Jun 26, 2009 (gmt 0)

10+ Year Member



If on a course I would recomend discussion in course forums to avoid chance of plagiarism.

Just something to think about really

gunter

7:50 am on Jun 29, 2009 (gmt 0)

10+ Year Member



Yeah, you are right Dizne

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>