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
Output Printed on Console.log