I'm trying to create a program in which an element can be added or removed from the Array, according to what the user places in the prompt command. I'm having a hard time removing elements from Array. Here's my program:
var regiaoAmazonica = ["Boca da Amazonia", "Vulcao", " Iracema", " Renato", "Macaco", " Santo Rafael", " Monte Mismi", " Mamiraua", " Tefe", " Alter Do Chao", " Manaus", " Rurrenabaque", " Rio Tabajós"];
var nomeDoGuia = [" João Figueiredo", " Emílio Médici", " Luiz Inácio Lula da Silva", " Dilma Rousseff", " Michel Temer", " Alejandro Toledo", " Alan García Pérez", " Pedro Pablo Kuczynski", " Maria Sanchez", " Ranieri Mazzilli", " Martín Vizcarra", " Elías Rodríguez", " Margaret Thatcher"];
var visitante = [" 4 estrelas", " 2 estrelas", " 2 estrelas", " 4 estrelas", " 0 estrelas", " 4 estrelas", " 2 estrelas", " 3 estrelas", " 0 estrelas", " 1 estrelas", " 3 estrelas", " 4 estrelas", " 2 estrelas"];
Add new regions. This code does not need correction:
var novaRegiaoAmazonica = prompt("Adicione uma nova regiao");
var novoNomeDoGuia = prompt("Entre o nome de um novo guia");
var novoVisitante = prompt("Entre sua nota para a regiao e o guia");
regiaoAmazonica.push(novaRegiaoAmazonica);
nomeDoGuia.push(novoNomeDoGuia);
visitante.push(novoVisitante);
var i = 0;
while(i < regiaoAmazonica.length){
document.write(regiaoAmazonica[i] + ", " + nomeDoGuia[i] + ", " + visitante[i] + '<br />');i++;
}
This is the code that is not working. You can enter the name of the region you want to delete (and consequently the other region-related information). If the region name does not exist, the user should receive an error message.
var deleteRegiaoAmazonica = prompt("Aqui esta a lista de regioes " + '\n' + amazonRegion + "Por favor digite a regiao que voce precisa remover da lista")
var deleteRegiaoAmazonica = function() {
var index = regiaoAmazonica.indexOf(deleteRegiaoAmazonica);
if (deleteRegiaoAmazonica === RegiaoAmazonica[i] && index>-1) {
RegiaoAmazonica.splice(index, 1);
nomeDoGuia.splice(index, 1);
visitante.splice(index, 1);
document.write(RegiaoAmazonica);
} else {
alert("Erro");
}
}