1. 程式人生 > >List of js

List of js

splice split tostring fin const () clear 王者歸來 元素

function List(arr) {
    this.listSize  = arr.length; //列表元素個數
    this.pos       = 0;          //列表當前位置  
    this.dataStore = arr;        //初始化一個空數組來保存列表元素
}
List.prototype = {
    constructor: List,
    append: function(element) {
        this.dataStore[this.listSize++] = element;
    },
    find: function (element) {
        for (var i = 0; i < this.dataStore.length; ++i) {
            if (this.dataStore[i] == element) {
                return i;
            }
        }
        return -1;
    },
    remove: function (element) {
        var foundAt = this.find(element);

        if (foundAt > -1) {
            this.dataStore.splice(foundAt, 1);
            --this.listSize;
            return true;
        }
        return false;
    },
    length: function() {
        return this.listSize;
    },
    toString: function() {
        return this.dataStore;
    },
    insert: function (element, after) {
        var insertPos = this.find(after);

        if (insertPos > -1) {
            this.dataStore.splice(insertPos + 1, 0, element);
            ++this.listSize;
            return true;
        }
        return false;
    },
    clear: function() {
        delete this.dataStore;
        this.dataStore = [];
        this.listSize = this.pos = 0;
    },
    contains: function (element) {
        for (var i = 0; i < this.dataStore.length; ++i) {
            
            if (this.dataStore[i] == element) {
                return true;
            }
        }
        return false;
    },
    front: function() {
        this.pos = 0;
    },
    end: function() {
        this.pos = this.listSize-1;
    },
    prev: function() {

        if (this.pos > 0) {
            --this.pos;
        } else {
            console.log(‘已到達首頁‘);
        }
    },
    next: function() {

        if (this.pos < this.listSize-1) {
            ++this.pos;
        } else {
            console.log(‘已到達末頁‘);
        }
    },
    currPos: function() {
        return this.pos;
    },
    moveTo: function(position) {
        this.pos = position;
    },
    getElement: function() {
        return this.dataStore[this.pos];
    },
    displayObjList: function(Objtype) {
        for (var pos = 0; pos < this.length(); pos++) {
            if (this.dataStore[pos] instanceof Objtype) {
               console.log(this.dataStore[pos]["name"] + ", " +
                   this.dataStore[pos]["movie"]);
            } else {
              continue;
            }
        }
    },
    showAllList: function() {
        for (var pos = 0; pos < this.length(); pos++) {
               console.log(this.dataStore[pos]);
        }
    },
    checkOut: function(name, movie, customerList) {

        if (this.contains(movie)) {
            var c = new Customer(name, movie);
            customerList.append(c);
            this.remove(movie);
        } else {
            console.log(movie + " is not available.");
        }
    }
}

//文件數據
var string = ‘(1)The Shawshank Redemption( 《肖申克的救贖》 )‘
           + ‘(2)The Godfather( 《教父》 )‘
           + ‘(3)The Godfather: Part II( 《教父 2》 )‘
           + ‘(4)Pulp Fiction( 《低俗小說》 )‘
           + ‘(5)The Good, the Bad and the Ugly( 《黃金三鏢客》 )‘
           + ‘(6)12 Angry Men( 《十二怒漢》 )‘
           + ‘(7)Schindler’s List( 《辛德勒名單》 )‘
           + ‘(8)The Dark Knight( 《黑暗騎士》 )‘
           + ‘(9)The Lord of the Rings: The Return of the King( 《指環王:王者歸來》 )‘  
           + ‘(10)Fight Club( 《搏擊俱樂部》 )‘
           + ‘(11)Star Wars: Episode V - The Empire Strikes Back( 《星球大戰 5:帝國反擊戰》 )‘
           + ‘(12)One Flew Over the Cuckoo’s Nest( 《飛越瘋人院》 )‘
           + ‘(13)The Lord of the Rings: The Fellowship of the Ring( 《指環王:護戒使者》 )‘
           + ‘(14)Inception( 《盜夢空間》 )‘
           + ‘(15)Goodfellas( 《好家夥》 )‘
           + ‘(16)Star Wars( 《星球大戰》 )‘
           + ‘(17)Seven Samurai( 《七武士》 )‘
           + ‘(18)The Matrix( 《黑客帝國》 )‘
           + ‘(19)Forrest Gump( 《阿甘正傳》 )‘
           + ‘(20)City of God( 《上帝之城》 )‘;
var movies = string.split(/\(\d+\)/g).splice(1);
var moviesList = new List(movies); //保存電影
var customers = new List([]); //保存租賃電影的顧客

//顧客類
function Customer(name, movie) {
    this.name = name;
    this.movie = movie;
};

console.log("Available movies is under follow: \n");
moviesList.showAllList();
var name = window.prompt(‘what is your name?‘);
var movie = window.prompt(‘which movies do your like to see?‘);
moviesList.checkOut(name, movie, customers);
console.log("Now available movies is under follow: \n");
moviesList.showAllList();

  

List of js