1
I am trying to create a program in which an element can be added or removed from the Array, according to what the user puts in the command prompt. I’m having trouble removing elements from the Array. Follow 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 needs no 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’s not working. The user can enter the name of the region he wants to delete (and consequently other information related to the region). If the region name does not exist, the user must 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");
}
}
An observation, in the Array has some indexes that start with a space:
"Boca da Amazonia", "Vulcao", " Iracema", " Renato"
, what can happen is the error return, because it was not informed the space in the Prompt.– NoobSaibot
@Wéllingthonm.Souza Updated code to predict space.
– Sam
Thank you very much!
– mrsmrs1