Reply to topic  [ 14 posts ] 
Help with my code. 
Author Message
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
So hey, sorry if this is the wrong place for this, but I havent posted here in a while :).

Learing javascript in Uni and need help with this program;

The troubling bit is the bit in bold italics, about teamScore > highScore then highScore = teamName. I need this to save the best team name and then show them as the winner at the end, now the first if statement works and the > works, but the == does not work, when I have 2 teams and they have the same score, the two teams do not show up in the final alert, only the highScore and not highScore2. Can anyone help? The whole code is below (bit im talking about in bold and italics);

p.s I have been told to do notes, as my lecturer thinks its good practice and you can tell whats going on for a 3rd party (hopefully)

<html>
<script>

//Author: Ben Stokoe
//Date: 23/10/2009
//Purpose: A Prototype Competition Scoring System

var numberOfTeams = 0; //Number of teams in the tournament
var nameOfTeam = ""; ///Name of the team
var numberOfMatches = 0; //Number of matches each team has played
var wonLostDrawn = ""; //To enter won, lost or drawn for each match
var won = 0; //Counts the number of wins entered into wonLostDrawn
var lost = 0; //Counts the number of loses entered into wonLostDrawn
var drawn = 0; //Counts the number of draws entered into wonLostDrawn
var error = 0; //Counts the number of errors input into wonLostDrawn
var teamScore = 0; //Works out the score for the team by adding 4 for win, 2 for draw and -1 for loss
var totalScore = 0; //Adds all team scores together to get a total score for all the teams
var averageScore = 0; //Works out the average score for a team (totalScore / numberOfTeams)
var highScore = ""; //Keeps the name of the highest scoring team in the competition so far
var highScore2 = ""; //Keeps the name of the second highest scoring team if it equals the score of the previous high score

//Prompt for user to enter number of teams
numberOfTeams = eval(prompt("Enter number of teams in the tournament"));

//For loop for number of teams in tournament and will run for loop inside for matches
for (var teams = 1; teams <= numberOfTeams; teams = teams + 1)
{
//Prompt for user to enter name of team
nameOfTeam = prompt("Please enter the name of the team");

//Prompt for user to enter the amount of games team has played
numberOfMatches = eval(prompt("Please enter the number of matches the team has played"));

//For loop to get user to enter won, lost or drawn for each match played
for (var matches = 1; matches <= numberOfMatches; matches = matches + 1)
{
//Prompt to get user to enter win, lost or drawn for each game
wonLostDrawn = prompt("Please enter won, lost or drawn for each game played");

//IF statement to add add how many won, lost or drawn
if ((wonLostDrawn == "won") || (wonLostDrawn == "Won"))
{
won = (won + 1); //If enter won add 1 to total games won
teamScore = (teamScore + 4); //Total score is increased by 4 (for win)
} else if ((wonLostDrawn == "lost") || (wonLostDrawn == "Lost"))
{
lost = (lost + 1); //If enter lost add 1 to total games lost]
teamScore = (teamScore - 1); //Total score is decreased by 1 (for loss)
} else if ((wonLostDrawn == "drawn") || (wonLostDrawn == "Drawn"))
{
drawn = (drawn + 1); //If enter drawn add 1 to total games drawn
teamScore = (teamScore + 2); //Total score is increased by 2 (for drawn)
} else //If neither won, lost or drawn is entered error message is displayed
{
error = (error + 1); //Adds 1 to amount of errors
alert("This is not a valid selection, only enter won, lost or drawn"); //Alerts user of error if won, lost or drawn isnt entered
}
}

//Add the team score to the total score for all teams for average score later on
totalScore = (totalScore + teamScore);

//Alert to display team name, numer of games they won, lost or drew, th number of errors input, total games played and total scored
alert("Team name " + nameOfTeam
+ "\nNumber of games won " + won
+ "\nNumber of games lost " + lost
+ "\nNumber of games drawn " + drawn
+ "\nNumber of errors input " + error
+ "\nTotal team score " + teamScore
+ "\nTotal games played " + numberOfMatches
+ "\nTotal score " + totalScore);

//If statement to save the team name with the highest score, or if the same score save to second high score variable
if (teamScore > highScore)
{
highScore = nameOfTeam;
} else if (teamScore == highScore)
{
highScore2 = nameOfTeam;
}


//Equal won, lost and drawn to 0 for next team (so number of wins / loses or draws does not carry on from last team)
won = 0;
lost = 0;
drawn = 0;

//Team score equal to 0 so team score for 1 team does not roll on to next
teamScore = 0;

}

//Work out average score achieved by a team for alert below
averageScore = (totalScore / numberOfTeams);

//Display number of teams, average score acheived by a team and team with highest score
alert("Number of teams in the tournament " + numberOfTeams
+ "\nAverage score achieved by a team in the tournament " + averageScore
+ "\nTeam(s) with highest score " + highScore + " and " + highScore2);


</script>
</html>

_________________
I had the last spam thread in the TMP. :)


Sat Oct 24, 2009 7:01 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
I don't know about how strict variables are in javascript... but you are setting a numerical value to a string......

//If statement to save the team name with the highest score, or if the same score save to second high score variable
if (teamScore > highScore)
{
highScore = nameOfTeam;
} else if (teamScore == highScore)
{
highScore2 = nameOfTeam;
}

why not have Team1 Score1 Team2 and Score2 and store those, that way you can set them in the correct order (so if the score is > 1 then 1 becomes 2 THEN you set 1 etc)

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sat Oct 24, 2009 7:23 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Tue May 05, 2009 3:29 pm
Posts: 7173
Reply with quote
Did you ask her yet? :lol:

_________________
timark_uk wrote:
That's your problem. You need Linux. That'll fix all your problems.
Mark


Sat Oct 24, 2009 7:24 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Linux_User wrote:
Did you ask her yet? :lol:

:lol:

QFT

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sat Oct 24, 2009 7:26 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
finlay666 wrote:
I don't know about how strict variables are in javascript... but you are setting a numerical value to a string......


I think they are pretty loose, but certainly comparing a string with a numeric value is going to cause problems.

Choose your variable names carefully - I think using highScore1 and highScore2 as name holders is the confusion here. Store that data in variables highScoringTeam1 and highScoringTeam2 and their appropriate scores in highScore1 and highScore2.

Code:
var highScore1 = 0;
var highScore2 = 0;
var highScoringTeam1 = "";
var highScoringTeam2 = "";

//More code

if (teamScore > highScore)
{
highScoringTeam1 = nameOfTeam;
} else if (teamScore == highScore)
{
highScoringTeam2 = nameOfTeam;
}

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Sat Oct 24, 2009 7:35 pm
Profile
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
Thanks all for replies.

Paul, thats a good idea, but then I dont define the value of high score, so the if statement will always be true....

I worked it out anyway, but thanks all!

_________________
I had the last spam thread in the TMP. :)


Sat Oct 24, 2009 8:50 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
paulzolo wrote:
Choose your variable names carefully


Indeed.

I got so fed-up with this sort of problem I've given myself a new rule (as suggested by an old lecturer).

strThisIsAString e.g. strTeamOneName
intThisIsANumber e.g. intHighScoreOne
charThisIsAChar e.g. charFirstLetter

You get the idea...

_________________
Image


Sun Oct 25, 2009 10:29 am
Profile
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
I've only just started programming, so choosing suitable variable names is new to me, but I am taking on these comments, as I did get confused half way through what the variables did!

_________________
I had the last spam thread in the TMP. :)


Sun Oct 25, 2009 12:40 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Nick wrote:
paulzolo wrote:
Choose your variable names carefully


Indeed.

I got so fed-up with this sort of problem I've given myself a new rule (as suggested by an old lecturer).

strThisIsAString e.g. strTeamOneName
intThisIsANumber e.g. intHighScoreOne
charThisIsAChar e.g. charFirstLetter

You get the idea...

At work we use a similar code.

The first letter is l, p or s (local, parameter, or shared) and the second is i, f, d, c, r (int, decimal (floating point), date, character and rowid (recid)).

i.e. lc-TeamName
li-HighScore
lc-FirstLetter

and so on.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Oct 26, 2009 7:06 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Fogmeister wrote:
At work we use a similar code.

The first letter is l, p or s (local, parameter, or shared) and the second is i, f, d, c, r (int, decimal (floating point), date, character and rowid (recid)).

i.e. lc-TeamName
li-HighScore
lc-FirstLetter

and so on.


So if you had a local long called CoolJ you could have ll-CoolJ? :D

Hungarian notation is good... but there are times it really lets itself down, keeping it simple is the best way, as well as commenting any variable declaration/initialisation with what it is used for etc.

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Mon Oct 26, 2009 9:31 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
finlay666 wrote:
Fogmeister wrote:
At work we use a similar code.

The first letter is l, p or s (local, parameter, or shared) and the second is i, f, d, c, r (int, decimal (floating point), date, character and rowid (recid)).

i.e. lc-TeamName
li-HighScore
lc-FirstLetter

and so on.


So if you had a local long called CoolJ you could have ll-CoolJ? :D

Hungarian notation is good... but there are times it really lets itself down, keeping it simple is the best way, as well as commenting any variable declaration/initialisation with what it is used for etc.
LOL :D

I knew there was somehting I missed. l is used for logical (ie boolean). but yes, ll-CoolJ, I might have to put that into my code next time I'm writing something.

:lol:

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Oct 26, 2009 10:09 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
finlay666 wrote:
Hungarian notation is good... but there are times it really lets itself down, keeping it simple is the best way, as well as commenting any variable declaration/initialisation with what it is used for etc.

Yes, I don't much care for Hangarian either - good variable names should tell you what data is stored, and from that the type inferred. Basic camel-humping FTW! (8-p)

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Mon Oct 26, 2009 1:16 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Some of the code I have to bug fix is a nightmare as it was written before temp-tables (part of Progress) were around so the common way to store data was in work files.

These are temp files that store text and can only be sorted alphabetically.

Because of the name ("work" file) the variables were also ("working" variables) as in not shared or whatever.

Consequently they all begin with "w-" no matter what type of data. I have been bug fixing stuff and got so frustrated with it that I've spent half an hour rewriting it into the new notation so I can understand it and then bug fix it :D

if w-x > w-y then assign w-a = w-b + w-c.

Especially annoying when this will work when > and + can be used for integers or strings!

i.e. "Hello" + " " + "World" = "Hello World".

and 2 + 3 = 5.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Oct 26, 2009 2:11 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Fogmeister wrote:
Some of the code I have to bug fix is a nightmare as it was written before temp-tables (part of Progress) were around so the common way to store data was in work files.

These are temp files that store text and can only be sorted alphabetically.

Because of the name ("work" file) the variables were also ("working" variables) as in not shared or whatever.

Consequently they all begin with "w-" no matter what type of data. I have been bug fixing stuff and got so frustrated with it that I've spent half an hour rewriting it into the new notation so I can understand it and then bug fix it :D

if w-x > w-y then assign w-a = w-b + w-c.

Especially annoying when this will work when > and + can be used for integers or strings!

i.e. "Hello" + " " + "World" = "Hello World".

and 2 + 3 = 5.

One of the ugliest and least resourceful ways of string formatting there lol

and why not use tmp- as a temp variable?

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Mon Oct 26, 2009 11:07 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 14 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.