0

The javascript encoded into my html is not working. It is supposed to find the best out of the players listed, and then alert who is the best. When I run it, all of the HTML and CSS aspects show, but there is no alert. Please help. It might just be a syntax error or something but please still help! Here is the code:

<script>
            var players = [
                {name: "Pat Moran", overall: 67, position: 1},
                {name: "Peter Webb", overall: 81, position: 1},
                {name: "Ramiro Ramirez", overall: 74, position: 1}
            ];

            function findBestPlayer() {
                var bestSoFar = 0;
                var bestPlayer;
                for (var i = 0; i < players.length; i++) {
                    if (players[i].overall > bestSoFar) {
                        bestPlayer = players[i];
                        bestSoFar = players[i].overall;
                    }
                }
                return best;
            }

            var bestPlayer = findBestPlayer();
            alert("Best player is " + bestPlayer.name + " with an overall of " + bestPlayer.overall ;
        </script>

4 Answers 4

1

            var players = [
                {name: "Pat Moran", overall: 67, position: 1},
                {name: "Peter Webb", overall: 81, position: 1},
                {name: "Ramiro Ramirez", overall: 74, position: 1}
            ];

            function findBestPlayer() {
                var bestSoFar = 0;
                var bestPlayer;
                for (var i = 0; i < players.length; i++) {
                    if (players[i].overall > bestSoFar) {
                        bestPlayer = players[i];
                        bestSoFar = players[i].overall;
                    }
                }

                return bestPlayer;
            }

            var bestPlayer = findBestPlayer();
            alert("Best player is " + bestPlayer.name + " with an overall of " + bestPlayer.overall) ;
The closing parenthesis is missing for the alert function. Edit : corrected the bestPlayer instead of best. Thanks to runner and joseph.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm answering this mobily, so I can't debug it, but in the function findBestPlayer you return best instead of bestPlayer.

Comments

1

return best; is not defined - use return bestPlayer; in that place, and include a brace after the alert end

Comments

1

here best is not defined and missing brackets in alert

 var players = [
                {name: "Pat Moran", overall: 67, position: 1},
                {name: "Peter Webb", overall: 81, position: 1},
                {name: "Ramiro Ramirez", overall: 74, position: 1}
            ];

            function findBestPlayer() {
                var bestSoFar = 0;
                var bestPlayer;
                for (var i = 0; i < players.length; i++) {
                    if (players[i].overall > bestSoFar) {
                        bestPlayer = players[i];
                        bestSoFar = players[i].overall;
                    }
                }
                return bestPlayer;
            }
          var bestPlayer = findBestPlayer();
            alert("Best player is " + bestPlayer.name + " with an overall of " + bestPlayer.overall) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.