Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions js/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
* @returns {any[]}
*/
function flatten(arr) {

let result = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]));

} else {
result.push(arr[i]);
}
}
return result;
}
console.log(flatten([[1,2,3,4],[1,[1,[1]]]])==[1,2,3,4,1,1,1])
console.log(flatten([[1, 2, 3, 4], [1, [1, [1]]]]) == [1, 2, 3, 4, 1, 1, 1])
15 changes: 11 additions & 4 deletions js/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
* @param {number} size
* @return {Array}
*/
var chunk = function(arr, size) {

};
console.log(chunk([1,2,3,4,5], 1)==[[1],[2],[3],[4],[5]])
var chunk = function (arr, size) {
const result = [];
let i = 0;
while (i < arr.length) {
result.push(arr.slice(i, i + size));
i += size;
}
return result;

}
console.log(chunk([1, 2, 3, 4, 5], 1));
21 changes: 21 additions & 0 deletions js/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@
*/
function hasStraightFlush(arr) {
// 返回 true 或 false
let newarr = [];
for (let i = 0; i < arr.length; i++) {
newarr.push(arr[i] % 13);
}
let arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let arr2 = [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
let arr3 = [26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38];
let arr4 = [39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51];
for (let i = 0; i < arr.length; i++) {
if (arr1.includes(arr[i])) {
arr1.splice(arr1.indexOf(arr[i] % 13), 1);
} else if (arr2.includes(arr[i])) {
arr2.splice(arr2.indexOf(arr[i] % 13), 1);
} else if (arr3.includes(arr[i])) {
arr3.splice(arr3.indexOf(arr[i] % 13), 1);

} else if (arr4.includes(arr[i])) {
arr4.splice(arr4.indexOf(arr[i] % 13), 1);
}

}
}
27 changes: 26 additions & 1 deletion js/4/solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@ function createGradeManager() {
* @param {Object} student
*/
addStudent(student) {
if (!student || typeof student !== 'object') return;
const { name, math, english } = student;
if (typeof name !== 'string' || typeof math !== 'number' || typeof english !== 'number') {
return;
}
students.push({ name, math, english });
},

/**
* 获取某一科目的平均分
* @param {"math"|"english"} subject
*/
getAverage(subject) {
let average = 0;
if (subject === 'math') {
let sum = 0;
for (const student of students) {
sum += student[subject];
}
average = students.length ? sum / students.length : 0;
return average;

}
if (subject === 'english') {
let sum = 0;
for (const student of students) {
sum += student[subject];
}
average = students.length ? sum / students.length : 0;
return average;
}
},

/**
Expand All @@ -32,6 +56,7 @@ function createGradeManager() {
* @returns {Array} 排序后的新数组
*/
getRankList(subject) {

},

/**
Expand All @@ -45,7 +70,7 @@ function createGradeManager() {
* 获取全部学生(返回副本)
*/
getAll() {

},
};
}
Binary file added web/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web/in .png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading