JavaScript --- array exercises

1. Create an object to record student grades, provide a method to add grades, and a method to display student average grades.
 

function  Warehouse() {
			this.formData = [];  // 学生成绩库
			this.add = add;  // 添加方法
			this.average = average; // 计算平均值
		}
		function add(arr) {
			this.formData.push(arr)
		}
		function average() {
			var total = 0;
			var averages = 0.0;
			var datas = []
			
			for (var i = 0; i < this.formData.length; ++i) {
				
				if(Array.isArray(this.formData[i])){ // 判断是否是数组
					for (var col = 0; col < this.formData[i].length; ++col) {
						total += this.formData[i][col];
					}
				}else{
					
					total = this.formData[i];
				}
				if(this.formData[i].length){
					averages = total / this.formData[i].length;
				}else{
					averages = total
				}
				
				
				datas.push(averages.toFixed(2))
				total = 0;
				averages = 0.0;
			}
			
			return datas
		}
		var thisWeek = new Warehouse();
		thisWeek.add(52);
		thisWeek.add([660,660,660,660]);
		thisWeek.add(61);
		thisWeek.add(65);
		thisWeek.add(55);
		thisWeek.add(50);
		thisWeek.add(52);
		thisWeek.add(49);
		console.log(thisWeek.average()); // 显示 ["52.00", "660.00", "61.00", "65.00", "55.00", "50.00", "52.00", "49.00"]

I regard each addition as adding all the grades of a student

 

2 Store a group of words in an array, and display these words in forward and reverse order respectively

 This is to use sort() to sort in the positive order and then reverse() to reverse the order

var names = ["David","Mike","Cynthia","Clayton","Bryan","Raymond"];
names.sort();
 names.reverse();
 console.log(names);

 

3 Modify the weeklyTemps object that appeared earlier in this chapter so that it can use a two-dimensional array to store useful monthly data. Add some methods to display monthly averages, specific weekly averages, and all week averages.

       


/***
 //二维数组来存储每月的有 用数据
//创建一个用以显示月平均数的方法
// 一周平均数
//所有周的平均数

*/
/**
 * 思路
 * 第一步 每月的有用数据 那么就是 每月四个周 初始化四个数组每组七个初始值
 * 第二步 月平均数的方法 average
 * 每周的
 * 所有周的 平均数
 */
function weekTemps() {
    this.dataStore = [];
    this.init = init;
    this.add = add;
    this.monthAverage = monthAverage;
    this.weekAverage = weekAverage;
    this.dayAverage = dayAverage;
}
function init() { /// 初始化 数组为二维数组格式
    var data = [];
    for (var i = 0; i < 4; i++) {
        var week = [];
        for (var j = 0; j < 7; j++) {
            week[j] = 0;
        }
        data[i] = week;
    }
    this.dataStore = data;
}
function add(week, day, temp) {  // 需要添加第几周 第几天 减一是对应数组的长度 可以输入 add(4,6,6767)  //第四周的第六天 插入6767
    let w = week - 1;
    let d = day - 1
    this.dataStore[w][d] = temp;
}
function monthAverage() { // 月平均数
    var total = 0;
    var sum = 0;
    for (var i = 0; i < this.dataStore.length; ++i) {
        for (let j = 0; j < this.dataStore[i].length; ++j) {
            total += this.dataStore[i][j]
            sum++;
        }
    }
    return total / sum;
}
function weekAverage() { // 所有周平均数
    var total = 0;
    for (var i = 0; i < this.dataStore.length; ++i) {
        for (let j = 0; j < this.dataStore[i].length; ++j) {
            total += this.dataStore[i][j]
        }
    }
    return total / this.dataStore.length;
}
function dayAverage(week) { // 一周平均数
    var total = 0;
    for (var i = 0; i < this.dataStore[week].length; ++i) {
        total += this.dataStore[week][i]
    }
    return total / this.dataStore[week].length;
}
var thisWeek = new weekTemps();
thisWeek.init()
print(thisWeek.dataStore)

thisWeek.add(2, 3, 45)
thisWeek.add(2, 4, 60)
thisWeek.add(1, 3, 5)
print(thisWeek.dataStore)
print(thisWeek.monthAverage().toFixed(2))
print(thisWeek.weekAverage().toFixed(2))
print(thisWeek.dayAverage(2).toFixed(2))



4 Create such an object, which stores the letters in an array, and uses a method to connect the letters together
and display them as a word.

function Str(){
	this.words = ["a", "p", "p", "l","e"];
	this.toStr = toStr;
}
function toStr(){
	return this.words.join('');
}
var obj = new Str();
console.log(obj.toStr());


 

Guess you like

Origin blog.csdn.net/wanghongpu9305/article/details/109911967