Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda