I'm trying to use indexOf() to get the index of a string inside an array, in one of the functions it works well, but it is not working in another function. One function is to add a player, the other one is to update a player score.Both use the player name as a parameter Here is my code:
let ScoreBoard = {
testEntry: ["Chuck Norris", 1000000000],
scoreList: []
}
let menu = 0
let playerName = ""
let playerScore = 0
while (menu != "4") {
menu = prompt("Type 1 to add a player, 2 to remove a player, 3 to update player's score or 4 to exit")
switch (menu) {
case "1":
playerName = prompt("What is the name of the player")
playerScore = parseInt(prompt("What is the player score"))
AddPlayer(playerName, playerScore)
console.log(ScoreBoard)
break;
case "2":
playerName = prompt("What is the name of the player");
playerScore = parseInt(prompt("What is the player score"))
RemovePlayer(playerScore, playerName)
console.log(ScoreBoard)
break;
case "3":
playerName = prompt("What is the name of the player");
playerScore = parseInt(prompt("What is the player score"))
let newScore = parseInt(prompt("What is the new score?"))
UpdateScore(playerScore, playerName, newScore)
console.log(ScoreBoard)
break;
}
}
function AddPlayer(name, score, highscore) {
highscore = ScoreBoard.testEntry
let newEntry = [name, score]
ScoreBoard.scoreList.push(newEntry)
return ScoreBoard
}
function RemovePlayer(score, name) {
let index = ScoreBoard.scoreList.indexOf(name)
ScoreBoard.scoreList.splice(index)
return ScoreBoard
}
function UpdateScore(score, name, newScore) {
let updatedScore = score + newScore
console.log(updatedScore)
let index = ScoreBoard.scoreList.indexOf(name)
console.log(index)
//let scoreIndex = ScoreBoard.scoreList.indexOf(score)
//console.log(scoreIndex)
ScoreBoard.scoreList.map(function (element){updatedScore = element[index]
return ScoreBoard
})
}
The logic of getting the index in both the RemovePlayer() function and UpdateScore() are the same, so I don't know what I'm doing wrong. Sorry if I'm not being very clear, Engliish is not my first language.