Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!