Reputation: 83
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>
Upvotes: 0
Views: 53
Reputation: 636
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) ;
Upvotes: 1
Reputation: 962
return best;
is not defined - use return bestPlayer;
in that place, and include a brace after the alert end
Upvotes: 1
Reputation: 254
I'm answering this mobily, so I can't debug it, but in the function findBestPlayer you return best instead of bestPlayer.
Upvotes: 1
Reputation: 1172
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) ;
Upvotes: 1