I am not posting this so someone can solve it for me, but becasue I am learning without much guidance,and I am sure I’ve skipped something that I should have known before I start practicing arrays and objects.(can you give me a hint to what I should learn?)
Every variable is globaly defined at the beggining of the code, so I have no idea why it’s givin this error.
Thats the error I am getting: Uncaught ReferenceError: printNames is not defined
This is my code:
HTML
<html>
<head>
<title>Player Screen</title>
<meta name="Description" content="Players info">
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<script scr="script.js"></script>
</head>
<body>
<div class="joined-players">
<div class="player-card" onclick="printNames();">
<div class="player-details">
<p class="player-name"></p>
<p class="player-score"></p>
</div>
<div class="player-img" onclick="changePlayerDetails();"></div>
</div>
</div>
</body>
</html>
and js
function Player(name, score)
{
this.name = name;
this.score = score
}
var players = new Array();
players.push(new Player("Player Array 1", 500));
players.push(new Player("Player Array 2", 20));
function printNames()
{
for(var i = 0; i != players.length; i++)
{
console.log(players[i].name);
console.log(players[i].score);
}
}
function changePlayerDetails()
{
var playerName = document.getElementsByClassName("player-name");
var playerScore = document.getElementsByClassName("player-score");
playerName.value = players[0].name;
playerScore.value = players[0].score;
}