Results 1 to 3 of 3

Thread: Help Needed - Loops problem

  1. #1
    Join Date
    Oct 2009

    Help Needed - Loops problem

    I'm trying to create a javascript program using nested-loops and conditional statements, that enters the Team's Name, the number of matches played and the number of goals the home and away teams scored.

    Which then displays a report in a single output window, that contains:
    • The team's name

    • Number of Matches won

    • Number of Matches lost

    • Number of Matches drawn

    • The point's scored

    Now I'm trying to extend the program by adding more team's and repeating the above process for each team.

    What I was expecting was that each team would have their own reports like
    Team Name: A
    Match's won: 1
    Match's lost: 0
    Match's Drawn: 2
    Final Score: 8

    Team Name: B
    Match's won: 2
    Match's lost: 1
    Match's Drawn: 0
    Final Score: 7
    However in the final output window it Combine's the two teams results so it would appear something like this:

    Team Name: B
    Match's won: 3
    Match's lost: 1
    Match's Drawn: 2
    Final Score: 15
    Here's the main body of the code:

    // Number of teams in competition
    numTeam = eval(prompt("Number of Teams in Competition"));

    // For loop
    for (var comp = 1; comp <= numTeam; comp = comp + 1)
    // Prompt for the team's name
    team_Name = prompt("What is the Team's Name?");

    // Matches played
    matches = eval(prompt("Number of matches played"));

    for (var game = 1; game <= matches; game = game + 1)
    // Prompts for Goals Scored
    goal1 = eval(prompt("Enter goal's scored for Team 1"));
    goal2 = eval(prompt("Enter goal's scored for Team 2"));

    // If-Then Else Statement to see if the team either, won, lost, or drew their game
    if (goal1 > goal2)
    winGame = winGame + 1;
    } else if (goal1 < goal2)
    loseGame = loseGame + 1;
    } else if (goal1 = goal2)
    drawGame = drawGame + 1;
    } else
    alert("There is an error")

    // Amount of points won
    win = winGame * pointsWin

    // Amount of points lost
    loss = loseGame * pointsLose

    // Amount of points from draws.
    draw = drawGame * pointsDraw

    // Total amount of points from wins, losses and draw's
    total = win + loss + draw

    alert("Team Name: " + team_Name + "\nNumber of matches Won: " + winGame + "\nNumber of matches Lost: " + loseGame + "\nNumber of matches Drawn: "
    + drawGame+ "\nFinal team score: " + total);
    I have some idea, where the problem is, however all solutions I can come up with still cause the same thing.

    Sorry If this seems long but I wanted to go into as much detail as I can, to help make things easier.

  2. #2
    Join Date
    Dec 2005


    I'm a little surprised that you get any output at all with the posted code.

    For a start, all of following need to be initialized prior to use in formulas
    var winGame = 0;
    var loseGame = 0;
    var drawGame = 0;
    var pointsWin = 0;
    var pointsLose = 0;
    var pointsDraw = 0;
    And since the pointsWin, pointsLose and pointsDraw are use to multiply
    they need to be set to something other than zero.

    Also, it is very difficult to debug when you need to re-enter information for each test pass. I would suggest starting with settings hard coded until debugged and avoid using the 'prompt' to input the data each time. Once you are satisfied that the calculations are correct, then put in to prompts for user input if desired.

    My suggestion would be to avoid the prompts altogether and use input boxes to fill in the names, scores, etc. That way if the user makes an error, they don't need to re-enter all the data again (and possibly make more input errors).

    My suggestion would be to input the information into an array for each team and do calculations AFTER all input has been completed.

  3. #3
    Join Date
    Jan 2009
    Another thing to look at is the extra use of "eval" before regular functions. You just plain don't need eval at all for what you are doing.

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