Jump to content

Javascript(function, Hates Me)


Sambabulli
 Share

Recommended Posts

So I'm working on this script for a class assignment that is due Monday afternoon, and I can't for the life of me get this switch to work...

Anybody know a javascript jedi who can look over my code and help me on the road to debugging this craptacular nonsense?

Edit for red, and cause I'm stupid:

//!--<script type="text/javascript">

function totalVotes(vote1-5) {

//calculate the sum of all values within an array.

var total = 0;

for (i = 1; i <= 5; i+=5);

return total;

}

function calcPercent(item, sum) {

//calculate a percentage, rounded to nearest integer

votes = item/sum*100(Math.round();

return votes

}

function createBar(partyType, percent) {

//write blank table cells for horizontal bar in election result

partyType = D, R, I, G, L;

percent = votes(Math.round();

switch (barText)

case "D": document.write(<td class='dem'></td>) ; break;

for (i = 1; i <= 5; i+=5);

case "R": document.write(<td class='rep'></td>) ; break;

for (i = 1; i <= 5; i+=5);

case "I": document.write(<td class='ind'></td>) ; break;

for (i = 1; i <= 5; i+=5);

case "G": document.write(<td class='green'></td>) ; break;

for (i = 1; i <= 5; i+=5);

case "L": document.write(<td class='lib'></td>) ; break;

for (i = 1; i <= 5; i+=5);

}

function showResults(race, name, party, votes) {

//shows the results of a particular race

var race = "race" document.write(

<h2>race</h2>

<table cellspacing='o'>

<tr>

<th>Candidate</th>

<th class='num'>Votes</th>

<th class='num'>%</th>

</tr>;

var name = "name":

<tr>

<td>name</td>

<td class='num'>votes</td>

</tr>;

var party = "party":

<tr>

<td>party</td>

<td class='num'>votes</td>

</tr>;

var votes = "votes":

<tr>

<td>votes</td>

<td class='num'>votes</td>;

var totalV = totalVotes(votes);

var percent ==calcPercent()

<td class='num'>(percent%)</td>;

createBar()

</tr>

</table>

</script>--//

Edited by Sambabulli
Link to comment
Share on other sites

Math.round syntax: http://www.javascriptkit.com/javatutors/round.shtml

Google should help with the rest, but even if all the syntax was correct, how will this work? Wheres the input values for all your variables coming from?

Link to comment
Share on other sites

Math.round syntax: http://www.javascriptkit.com/javatutors/round.shtml

Google should help with the rest, but even if all the syntax was correct, how will this work? Wheres the input values for all your variables coming from?

/*

Filename: votes.js

Variable List:

races: The name of the five races

name1 - name5: The name of the candidate for races 1 through 5

party1 - party5: The party affliations of the candidates

votes1 - votes5: The number of votes the candidate has received

*/

var race = new Array();

var name1 = new Array();

var name2 = new Array();

var name3 = new Array();

var name4 = new Array();

var name5 = new Array();

var party1 = new Array();

var party2 = new Array();

var party3 = new Array();

var party4 = new Array();

var party5 = new Array();

var votes1 = new Array();

var votes2 = new Array();

var votes3 = new Array();

var votes4 = new Array();

var votes5 = new Array();

race[0]="1st Congressional District";

race[1]="2nd Congressional District";

race[2]="3rd Congressional District";

race[3]="4th Congressional District";

race[4]="5th Congressional District";

name1[0]="Sarah Carlson";

party1[0]="D";

votes1[0]=45125;

name1[1]="Pete deJesus";

party1[1]="R";

votes1[1]=44498;

name1[2]="Alan Tompkins";

party1[2]="I";

votes1[2]=5143;

name2[0]="Gary Griffin";

party2[0]="D";

votes2[0]=69505;

name2[1]="Frank Brown";

party2[1]="R";

votes2[1]=78133;

name2[2]="Roland Washington";

party2[2]="G";

votes2[2]=8109;

name2[3]="Karen Reese";

party2[3]="L";

votes2[3]=13004;

name3[0]="Anne Sanchez";

party3[0]="D";

votes3[0]=65203;

name3[1]="Cynthia Thomas";

party3[1]="R";

votes3[1]=51289;

name4[0]="Jerry Wilkes";

party4[0]="D";

votes4[0]=49201;

name4[1]="Barry Mitchell";

party4[1]="R";

votes4[1]=58414;

name4[2]="Paula Welton";

party4[2]="I";

votes4[2]=3901;

name5[0]="Pete Grimbold";

party5[0]="D";

votes5[0]=42105;

name5[1]="Carol Ives";

party5[1]="R";

votes5[1]=43349;

name5[2]="Michael Dorn";

party5[2]="G";

votes5[2]=1401;

----------------------------------

Sorry if my questions are noobish, the shoe fits. I'm wearing it.

In a year, I'll be a lot smarter, and wiser. But frankly, this is my first real programming class (I don't consider Oracle/SQL programming necessarily). I feel like I'm missing something, but I can't figure out what it is.

partyType = D, R, I, G, L; has me most confused at this point.

How is the code telling the var partyType which one of the 5 choices is the correct one? I know that partyType and percentage are the parameters of the function. I just don't know what mechanism to establish for partyType.

I'm sure there are other major flaws, but that's where my mind is stuck at this stage.

Edited by Sambabulli
Link to comment
Share on other sites

function totalVotes(vote1-5) {
//calculate the sum of all values within an array.
var total = 0;
for (i = 1; i &lt;= 5; i+=5);
return total;
}

What is this I don't even.

I'm not entirely certain myself; but I could show you the bizarre way it was written in the book. Obviously I missed something, so that would be a very good place to start.

On the other hand, I'm new here, so I'm really struggling not to ask if you broke that sentence up into something that would make me want to eat broken glass, or if you really thought that was a complete, and well-structured sentence and I was just suppose to overlook it.

Either way, cheers.

I'll let you know if I get this working tonight or not.

Link to comment
Share on other sites

On the other hand, I'm new here, so I'm really struggling not to ask if you broke that sentence up into something that would make me want to eat broken glass, or if you really thought that was a complete, and well-structured sentence and I was just suppose to overlook it.

What is this I don't even.

It's a meme, welcome to the internet, you'll see them around alot.

Link to comment
Share on other sites

BattZ,

Maybe I was too complex, and therefore what I said was misread. Lemme see if I can redirect this better:

I see wut u did thar?

obvsly.

(hint: Perhaps I didn't think the question was so idiotic as to warrant a "you're an idiot for asking a question" response)

Somehow I didn't think this was the WoW forums; In hindsight, perhaps I should always carry myself with the same razor-edged paranoid mentality that is apparently mandatory to ask a valid question.

Link to comment
Share on other sites

While I'm quite tired now, I feel I might have missed something, but I do feel like I was being more ass-ish then I was attempting to be, and for that I apologize.

While Javascript isn't my first language, I'll give this a shot.

function totalVotes(vote1-5) {

I don't think you can use - in variable names

Is that supposed to be vote1, vote2...vote5 or is that the variable name?

whichever it is, it isn't used in the function.

for (i = 1; i &lt;= 5; i+=5);

This bit of code kinda confuses me, it's a loop that is hard-coded to only run through once. Also, it doesn't execute any code, since the ; is right after and not {CODE GOES HERE} like normal.

var total = 0;
return total;

Sets the variable total to 0, then returns it, while normal in some situations, like to be sure the function ran correctly.

I hope you got this figured out before it was due.

Good Luck

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...