JavaScript数据结构与算法-数组练习

一. 创建一个记录学生成绩的对象,提供一个添加成绩的方法,以及一个显示学生平均成绩的方法。

// 创建一个记录学生成绩的对象
const Students = function Students () {
    this.grades = [];
    this.addGrage = addGrade;
    this.getAverage = getAverage;
};
// 提供一个添加成绩的方法
function addGrade (grade) {
    this.grades.push(grade);
}
// 以及一个显示学生平均成绩的方法
function getAverage () {
    let total = 0;
    let len = this.grades.length;
    while (len--) {
        total += parseInt(this.grades[len], 10);
    }
    return total / this.grades.length;
}
// 示例
let Mazey = new Students();
Mazey.addGrage(90);
Mazey.addGrage(91);
Mazey.addGrage(87);
Mazey.addGrage(99);
Mazey.addGrage(89);
Mazey.addGrage(85);
Mazey.addGrage(92);
console.log(Mazey.getAverage());

二. 将一组单词存储在一个数组中,并按正序和倒序分别显示这些单词。

// 将一组单词存储在一个数组中
let arr = [`Mazey`, `Cherrie`, `John`, `Ada`, `Chole`];
// 正序
console.log(arr.sort());
// 倒序
console.log(arr.sort().reverse());

三. 创建一个对象,使用一个二维数组来存储每月的有用数据,增加一些方法用以显示月平均数,具体某一周平均数和所有周的平均数。

const MonthData = function MonthData () {
    // 初始化月数据
    this.monthData = (function () {
        let arr = [];
        let week = 4;
        let day = 7;
        while (week--) {
            arr[week] = [];
            while (day--) {
                arr[week][day] = 0;
            }
            day = 7;
        }
        console.log(arr);
        return arr;
    })();
    this.addData = addData;
    this.getMonthAverage = getMonthAverage;
    this.getWeekAverage = getWeekAverage;
    this.getAllWeekAverage = getAllWeekAverage;
};
function addData (week, day, data) {
    this.monthData[week][day] = data;
}
function getMonthAverage () {
    let arr = this.monthData,
        total = 0,
        day = 0;
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr[i].length; j++) {
            total += parseInt(arr[i][j], 10);
            day++;
        }
    }
    return total / day;
}
function getWeekAverage (week) {
    let arr = this.monthData[week],
        total = 0;
    for (let i = 0; i < arr.length; i++) {
        total += parseInt(arr[i], 10);
    }
    return total / arr.length;
}
function getAllWeekAverage () {
    let self = this,
        week = this.monthData.length,
        total = (function () {
            let total = 0;
            for (let i = 0; i < week; i++) {
                total += self.getWeekAverage(i) * self.monthData[i].length
            }
            return total;
        })();
        return total / week;
}
// 示例
let Mazey = new MonthData();
Mazey.addData(0, 1, 100);
Mazey.addData(0, 2, 100);
Mazey.addData(0, 3, 100);
Mazey.addData(0, 4, 100);
Mazey.addData(1, 6, 7);
console.log(Mazey.getMonthAverage());
console.log(Mazey.getWeekAverage(0));
console.log(Mazey.getWeekAverage(1));
console.log(Mazey.getAllWeekAverage());

四. 创建这样一个对象,它将字母存储在一个数组中,并且用一个方法可以将字母连在一起,显示成一个单词。

const Letter = function Letter () {
    this.characters = [];
    this.addCharacter = addCharacter;
    this.toLetter = toLetter;
};
function addCharacter (character) {
    this.characters.push(character);
}
function toLetter () {
    return this.characters.join(``);
}
// 示例
let Mazey = new Letter();
Mazey.addCharacter(`m`);
Mazey.addCharacter(`a`);
Mazey.addCharacter(`z`);
Mazey.addCharacter(`e`);
Mazey.addCharacter(`y`);
console.log(Mazey.toLetter()); // mazey

发表评论

电子邮件地址不会被公开。 必填项已用*标注