算法与数据结构学习
This commit is contained in:
51
Chapter2/my.mjs
Normal file
51
Chapter2/my.mjs
Normal file
@ -0,0 +1,51 @@
|
||||
// 对数组进行复制,非引用赋值
|
||||
function copy(arr) {
|
||||
const arr2 = [];
|
||||
const len = arr.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
arr2[i] = arr[i];
|
||||
}
|
||||
|
||||
return arr2;
|
||||
}
|
||||
|
||||
// 关于sort排序
|
||||
// 数字排序方式,从小到大升序排列
|
||||
// sort函数接受的排序方法,返回的值应当为负数、0、正数,排序也按照这个顺序排序
|
||||
function sortNumber(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
// 获取奇数
|
||||
function isOdd(num) {
|
||||
return num % 2 !== 0;
|
||||
}
|
||||
|
||||
// 获取偶数
|
||||
function isEven(num) {
|
||||
return num % 2 === 0;
|
||||
}
|
||||
|
||||
const nums = [];
|
||||
for (let i = 0; i < 20; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
|
||||
const oddList = nums.filter(isOdd);
|
||||
const evenList = nums.filter(isEven);
|
||||
|
||||
console.log(oddList);
|
||||
console.log(evenList);
|
||||
|
||||
// 创建二维数组(表格)
|
||||
Array.matrix = function(numrows, numcols, initval) {
|
||||
const arr = [];
|
||||
for (let i = 0; i < numrows; i++) {
|
||||
const columns = [];
|
||||
for (let j = 0; j < numcols; j++) {
|
||||
columns[j] = initval;
|
||||
}
|
||||
arr[i] = columns;
|
||||
}
|
||||
return arr;
|
||||
}
|
64
Chapter2/test.mjs
Normal file
64
Chapter2/test.mjs
Normal file
@ -0,0 +1,64 @@
|
||||
// 1
|
||||
// 创建一个记录学生成绩的对象,提供一个添加成绩的方法,以及一个显示学生平均成绩的方法。
|
||||
function createTranscript() {
|
||||
this.gradeList = [];
|
||||
this.add = (num) => {
|
||||
this.gradeList.push(num);
|
||||
};
|
||||
this.calcAvge = () => {
|
||||
const sum = this.gradeList.reduce((sum, grade) => {
|
||||
sum += grade;
|
||||
return sum;
|
||||
}, 0);
|
||||
return sum / this.gradeList.length;
|
||||
};
|
||||
}
|
||||
|
||||
const transcript1 = new createTranscript();
|
||||
transcript1.add(120);
|
||||
transcript1.add(80);
|
||||
console.log(transcript1.calcAvge());
|
||||
|
||||
// 2
|
||||
// 将一组单词存储在一个数组中,并按正序和倒序分别显示这些单词
|
||||
const list = ["fan", "sheng", "fa", "lan", "xiu", "ping"];
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
console.log(list[i]);
|
||||
}
|
||||
console.log("--------------------------")
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
console.log(list[i]);
|
||||
}
|
||||
|
||||
// 3
|
||||
function weekTemps() {
|
||||
this.dataStore = [];
|
||||
for (let i = 0; i < 12; i++) {
|
||||
this.dataStore.push([])
|
||||
}
|
||||
this.add = add;
|
||||
this.average = average;
|
||||
}
|
||||
|
||||
function add(month, temp) {
|
||||
this.dataStore[month].push(temp);
|
||||
}
|
||||
|
||||
function average() {
|
||||
var total = 0;
|
||||
for (var i = 0; i < this.dataStore.length; ++i) {
|
||||
total += this.dataStore[i];
|
||||
}
|
||||
return total / this.dataStore.length;
|
||||
}
|
||||
|
||||
var thisWeek = new weekTemps();
|
||||
thisWeek.add(52);
|
||||
thisWeek.add(55);
|
||||
thisWeek.add(61);
|
||||
thisWeek.add(65);
|
||||
thisWeek.add(55);
|
||||
thisWeek.add(50);
|
||||
thisWeek.add(52);
|
||||
thisWeek.add(49);
|
||||
print(thisWeek.average()); // displays 54.875
|
Reference in New Issue
Block a user