www.webdeveloper.com
Results 1 to 4 of 4

Thread: stuck on javascript program

  1. #1
    Join Date
    Jun 2009
    Posts
    2

    stuck on javascript program

    can anyone help me please , and tell me whats wrong with this program? just keep getting NAN


    <HTML>
    <HEAD>
    <TITLE></TITLE>

    <SCRIPT

    language="JavaScript"
    type="text/javascript">

    /* Program to summarise the results of the election for Secretary.
    */

    //variables

    var totalMembership, numberOfCandidates, totalVote; candidateName; candidateVote;
    var candidateNameArray = [];
    var candidateVoteArray = [];

    var candidateName = new Array(candidateNameArray);
    var candidateVote = new Array(candidateVoteArray);

    totalVote = 0;

    // User enters the number of Candidates

    numberOfCandidates = window.prompt('Please input the number of Candidates', ' ');
    numberOfCandidates = parseFloat(numberOfCandidates);

    numberOfCandidate = new Array(candidateNameArray);
    for (var count = 0; count < numberOfCandidates; count = count + 1)
    {
    numberOfCandidates[count] = numberOfCandidates[count] + window.prompt('Please input candidate name ' + (count + 1));
    }
    for (var count = 0; count < numberOfCandidates; count = count + 1)
    {
    candidateVoteArray[count] = candidateVoteArray[count] + window.prompt('Please input the number of votes candidate recieved ' + (count + 1));
    candidateVoteArray = parseFloat(candidateVoteArray);

    totalVote = totalVote + candidateVoteArray[count];
    }
    // Total votes output

    document.write('Total votes cast: ' + totalVote + '<BR>');

    /*Each candidates percentages are calculated*/

    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>');
    }

    /* Working out the and displaying the winning candidate */

    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>

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>anj</TITLE>

    <
    SCRIPT type="text/javascript">
    var 
    candidates;

    function 
    getCandidates(c){    candidates=c.split(',');
     var 
    nameVote='<table width="250px"><tr style="color:#000066; font-family:Arial; font-size:18px; font-weight:bold;">';
       
    nameVote+='<td colspan=2>Please ascribe the number of votes garnered by each candidate  <i>(or leave as is)</i></td></tr>';
        for(
    i=0;i<candidates.length;i++){nameVote+='<tr><td>'+candidates[i]+'</td><td><input size=1 value="'+Math.floor(200*(Math.random()))+'"></td></tr>'}
        
    document.forms[1].innerHTML=nameVote+'</table><input type=submit>'
        
    }
    function 
    getVotes(){var cv=document.forms[1].elements; var sum=0; for(i=0;i<cv.length-1;i++){sum+=Number(cv[i].value);}
            var 
    tots=new Array(); var t='<table><tr><th>candidate</th><th> votes </th><th> % </th></tr>';
        for(
    i=0;i<candidates.length;i++){t+='<tr><td>'+candidates[i]+'</td><td>'+cv[i].value+'</td><td>'+(cv[i].value/sum).toFixed(2)+'</td></tr>'}
            
    t+='<tr><td colspan=3 align="center">total votes= '+sum+'</td></tr>'
        
    document.getElementById('results').innerHTML=t+'</table>'
    }

    </SCRIPT>

    </HEAD>

    <BODY>
    Greetings sirs,<BR> We wish not to provoke your ire with malicious prompts so,
     enter the names of all the candidates in the textarea below as is the example ( or just leave it as is)
    <form onSubmit="getCandidates(document.getElementById('txC').value); return false">

    <textarea id="txC" rows=4 cols=60>John Kennedy, Richard Nixon, Howard Taft, Joe 6pac</textarea>
    <input type=submit>
    </form>
    <form id="votes" onSubmit="getVotes(); return false"></form>
    <div id="results"></div>
    </BODY>

    </HTML> 

  3. #3
    Join Date
    Jun 2009
    Posts
    2

    Stuck on javascript

    wow, thank you for your reply.
    Thats a very complex program you have written. Is there anyway of simplifying it by ammending a few points to my program ..

    Am very greatful you went to all the trouble of writting it out for me

  4. #4
    Join Date
    Oct 2006
    Posts
    939
    Hi,
    I wrote it on the fly in about ten minutes just as I would write text, like now.
    Leave out "var tots=new Array();" It doesn't do anything.
    I hope there is something to learn there, Number() is something you might use
    But I'm not going to wrestle with those prompts. Good luck

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles