En my displayList(), la opción The next() debería detenerse una vez que alcance el último registro de la lista de películas que se lee del archivo films.txt. Intentando mostrar la lista de elementos en una matriz. El método next() se ejecuta para ser un bucle infinito. ¿Alguien tiene una sugerencia mejor? Gracias
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);Los datos de films.txt están debajo
Salida impresa en Console.log