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

167
Visualizações
Read a text file using Javascript next() goes into an infinite loop

In my displayList() the The next() option should stop once it reaches the last record from the Movies list which is read from the films.txt file. Trying to display the list of items in an array. The next() method runs to be an infinite loop. Does anyone have a better suggestion. Thank you

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    this.length = length;
    this.toString = toString;
    this.append = append;
    this.front = front;
    this.currPos = currPos;
    this.next = next;
    this.getElement = getElement;
}

function append(element){
    this.dataStore[this.listSize++] = element;
 }

 function length(){
    return this.listSize;
 }
 
 function toString(){
    return this.dataStore;
 }
 function front(){
    this.pos = 0;
 }
 function next(){
    if(this.pos < this.listSize - 1){
        ++this.pos;
    }
 }
 
 function currPos(){
    return this.pos;
 }

 function next(){
    if(this.pos < this.listSize - 1){
        ++this.pos;
    }
 }
  
 function getElement(){
    return this.dataStore[this.pos];
 }

function createArr(file) {
    const { readFileSync } = require('fs');
    const arr = readFileSync(file).toString().replace(/\r\n/g,'\n').split('\n');
    for(let i =0; i< arr.length; ++i){
       arr[i] =arr[i].trim();
    }
    return arr;
 }

 function displayList(list){
    for(list.front(); list.currPos() < list.length(); list.next()){        
            console.log(list.getElement());    
    }
    
}

let movies = createArr("films.txt");
let movieList = new List();
for(let i= 0 ; i < movies.length; ++i )
{
    movieList.append(movies[i]);
}
console.log("Available movies: \n");
displayList(movieList);

Data from films.txt Is below

  • The Shawshank Redemption
  • The God Father
  • Pulp Fiction
  • The Good, the bad and the Ugly
  • 12 Angry Men

Output Printed on Console.log

  • The Shawshank Redemption
  • The God Father
  • Pulp Fiction
  • The Good, the bad and the Ugly
  • 12 Angry Men
  • 12 Angry Men
  • 12 Angry Men
  • 12 Angry Men
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