How to correctly insert data in a Javascript queue?

Asked

Viewed 78 times

0

I have to enter some data in a queue, and I have the following functions :

Insert Rpos - which will insert the data position,

Removerpos - which will remove the data position,

Lerlist - which will read the list that has been added.

My code :

<!DOCTYPE html>
<html>
<head>
<!-- <title>Web Page Design</title> -->
<script type = "text/javascript" />
function Lista(){

this.minhalista= new Array();
this.InserirPos= function(obj, pos){
pos--;
if(pos< this.minhalista.length) {
this.minhalista.splice(0,0,obj);
}
else{ 
alert("Posição invalida");
}
}

this.RemoverPos = function(pos){
if(pos<(this.minhalista.length+1)) {
this.minhalista.splice((pos-1),1);
}
}

this.LerLista = function(){
if(this.minhalista.length>0){
for(co=0;co< this.minhalista.length;co++)
document.write(this.minhalista[co]+"</br>");
}else{
alert("Não há objetos na Lista");
}
}
</script>
</head>

<body>

<h1>Exemplo Lista</h1>
<script type ="text/javascript" />

var minhalista= new Lista();

minhafila.InserirPos(prompt ("Digite um texto : "));
minhafila.InserirPos(prompt ("Digite um texto : "));
minhafila.InserirPos(prompt ("Digite um texto : "));

minhalista.LerLista();
</script>

</body>

</html>

The problem is that when putting in the HTML part to insert the positions and data, nothing happens and I can’t ask the user to insert the numbers, and even after this, it does not appear on the screen the numbers.

How can I fix this, and request and enter the data correctly in the queue ?

  • 1

    Is to instantiate minhalista : var minhalista= new Lista(); and then use as minhafila, and that’s one of the reasons why it doesn’t even work, and it lacks a } at the end for the function Lista. But the code you have does not play with the normal logic of a Queue. Explain better what each method is supposed to do.

  • What are you supposed to InserirPos do? add input to array?

  • have you looked at the browser debug you are using? see if it returns any errors

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.