The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

student programmer can u check my code plse

Hello all on the forum, I hope you can help as my tutor doesn't seem contactable at present, I have an assignment which I don't expect you to do for me but feel I'm going around in circles - and looking for someone to point me in the right direction. It is a voting system that gets a vote, then gets remaining vote and calculates how many each candidate was voted for from the record I have i tried the charAt method to separate the individual votes but I'm sure there must be a better way.

//
// ***** YOU DO NOT NEED TO MODIFY THIS FUNCTION *****
// A FUNCTION TO REMOVE ONE OR MORE VOTES FROM THE FRONT OF A VOTING
// RECORD ONCE THEY HAVE BEEN COUNTED BY ANOTHER FUNCTION.
// ***** YOU DO NOT NEED TO MODIFY THIS FUNCTION *****
//
function getRemainingVotes(allTheVotes, lengthOfBallot)
{
allTheVotes = allTheVotes.substr(lengthOfBallot, allTheVotes.length - lengthOfBallot);
return (allTheVotes);
}

//
// THIS PROGRAM TAKES THE CONTENTS OF THE VOTING RECORD AND SENDS
// THEM TO THE TWO FUNCTIONS getNextVote() AND getRemainingVotes()
// FOR PROCESSING AFTER ALL VOTES HAVE BEEN CANCELLED IT DISPLAYS
// THE RESULTS OF THE ELECTION
//
function countVotes ()
{
var someVotes = ''; // THE COMPLETE VOTING RECORD FROM THE MACHINE
var theVote = ''; // A SINGLE VOTE FROM THE VOTING RECORD

document.write('<P>The results of the election are as follows\/P>');
document.write('The number of votes cast for '+ candidateName[0] + ' in the string is ' + candidateNumber[0] + '<BR>');
document.write('The number of votes cast for '+ candidateName[1] + ' in the string is ' + candidateNumber[1] + '<BR>');
document.write('The number of votes cast for '+ candidateName[2] + ' in the string is ' + candidateNumber[2] + '<BR>');
document.write('The number of votes cast for '+ candidateName[3] + ' in the string is ' + candidateNumber[3] + '<BR>');
document.write('The number of votes cast for all candidates in the string is ' + totalVote + '<BR>');
document.close(); // CLOSE THE PAGE AFTER WRITING THE RESULTS
}
</SCRIPT>
</HEAD>
<!--
YOU DO NOT NEED TO MODIFY, OR EVEN UNDERSTAND, ANY OF THE HTML BELOW THIS POINT
-->
<BODY>
<form action="" method="get" name="votingForm">
<input name="votingRecord" type="text" size="40" maxlength="255">
<input name="countButton" type="button" value="Count the votes" onClick="javascript:countVotes();">
</form>
</BODY>
</HTML>

comments

Hi featch1, welcome to the forums,
When I start a new project, I write the steps as comments in outline form. eg.
/* database connection code here */
/* get GET vars and sanitize */
/* assign GET var vals to db vars */
/* query string here */
/* perform db stuff */
/* HTML stuff here */
then I fill in the code step by step.
I often have better luck if I spend some time thinking about what steps are needed to do what I want to do first, before jumping right into the code first.
Nothing personal, but it seems that although you do have an idea of what you want the code to do, you are missing some crucial steps in the code logic.

Hi featch1, welcome to the forums,
When I start a new project, I write the steps as comments in outline form. eg.
/* database connection code here */
/* get GET vars and sanitize */
/* assign GET var vals to db vars */
/* query string here */
/* perform db stuff */
/* HTML stuff here */
then I fill in the code step by step.
I often have better luck if I spend some time thinking about what steps are needed to do what I want to do first, before jumping right into the code first.
Nothing personal, but it seems that although you do have an idea of what you want the code to do, you are missing some crucial steps in the code logic.

Hi Mittineaque, thanks for the advice as its an assignment I have to code it in the format given - the countVotes() is where I'm getting stuck but still playing with it - I'm now looking at two for loops but need to do something with the candidate array.

<HTML>
<HEAD>

<TITLE>A first-past-the-post vote counting program</TITLE>
<SCRIPT language = "JavaScript">
// REPLACEMENT FILE
//
// A FUNCTION TO EXTRACT ONE OR MORE VOTES FROM THE FRONT OF //VOTING RECORD

// THIS PROGRAM TAKES THE CONTENTS OF THE VOTING RECORD AND //SENDS THEM TO THE TWO FUNCTIONS getNextVote() AND //getRemainingVotes() FOR PROCESSING AFTER ALL VOTES HAVE BEEN //CANCELLED IT DISPLAYS THE RESULTS OF THE ELECTION

function countVotes()

{
var someVotes = ''; // THE COMPLETE VOTING RECORD FROM THE
MACHINE
var theVote = ''; // A SINGLE VOTE FROM THE VOTING RECORD

// GET THE VOTING RECORD FROM THE TEXT BOX IN THE HTML PAGE
//
someVotes = document.votingForm.votingRecord.value;

document.write('<P>The results of the election are as follows/P>');
document.write('Total number of votes cast was ' + totalVote + ' <BR>');
document.write('<BR>'+'No of votes for candidateName[0] is ' +
count);

document.close(); // CLOSE THE PAGE AFTER WRITING THE RESULTS
}
}
</SCRIPT>
</HEAD>
<!--
YOU DO NOT NEED TO MODIFY, OR EVEN UNDERSTAND, ANY OF THE HTML BELOW THIS
POINT
-->
<BODY>
<form action="" method="get"
name="votingForm">
<input name="votingRecord" type="text"
size="40" maxlength="255">
<input name="countButton" type="button"
value="Count
the votes" onClick="javascript:countVotes();">
</form>
</BODY>
</HTML>