Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
Javascript indexOf() not working with array

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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda