x404.co.uk http://x404.co.uk/forum/ |
|
Help with my code. http://x404.co.uk/forum/viewtopic.php?f=3&t=3645 |
Page 1 of 1 |
Author: | KRKux [ Sat Oct 24, 2009 7:01 pm ] |
Post subject: | Help with my code. |
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> |
Author: | finlay666 [ Sat Oct 24, 2009 7:23 pm ] |
Post subject: | Re: Help with my code. |
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) |
Author: | Linux_User [ Sat Oct 24, 2009 7:24 pm ] |
Post subject: | Re: Help with my code. |
Did you ask her yet? ![]() |
Author: | finlay666 [ Sat Oct 24, 2009 7:26 pm ] | |||||||||
Post subject: | Re: Help with my code. | |||||||||
![]() QFT |
Author: | paulzolo [ Sat Oct 24, 2009 7:35 pm ] | ||||||||||||||||||
Post subject: | Re: Help with my code. | ||||||||||||||||||
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.
|
Author: | KRKux [ Sat Oct 24, 2009 8:50 pm ] |
Post subject: | Re: Help with my code. |
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! |
Author: | Nick [ Sun Oct 25, 2009 10:29 am ] | |||||||||
Post subject: | Re: Help with my code. | |||||||||
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... |
Author: | KRKux [ Sun Oct 25, 2009 12:40 pm ] |
Post subject: | Re: Help with my code. |
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! |
Author: | Fogmeister [ Mon Oct 26, 2009 7:06 am ] | ||||||||||||||||||
Post subject: | Re: Help with my code. | ||||||||||||||||||
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. |
Author: | finlay666 [ Mon Oct 26, 2009 9:31 am ] | |||||||||
Post subject: | Re: Help with my code. | |||||||||
So if you had a local long called CoolJ you could have ll-CoolJ? ![]() 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. |
Author: | Fogmeister [ Mon Oct 26, 2009 10:09 am ] | ||||||||||||||||||
Post subject: | Re: Help with my code. | ||||||||||||||||||
![]() 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. ![]() |
Author: | EddArmitage [ Mon Oct 26, 2009 1:16 pm ] | |||||||||
Post subject: | Re: Help with my code. | |||||||||
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) |
Author: | Fogmeister [ Mon Oct 26, 2009 2:11 pm ] |
Post subject: | Re: Help with my code. |
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 ![]() 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. |
Author: | finlay666 [ Mon Oct 26, 2009 11:07 pm ] | |||||||||
Post subject: | Re: Help with my code. | |||||||||
One of the ugliest and least resourceful ways of string formatting there lol and why not use tmp- as a temp variable? |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |