Files
2024-07-04 22:38:39 +08:00

51 lines
1022 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 对数组进行复制,非引用赋值
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;
}