//循环实现
/* let arr = [5, 8, 9, 2, 1, 5]
let sum = 0
for (let i = 0; i < arr.length; i++) {
sum += arr[i]
}
console.log(sum); */
// 套函数实现
function ayy(arr) {
let sum = 0
let sum1 = 0
for (let i = 0; i < arr.length; i++) {
sum += arr[i]
}
sum1 = sum / arr.length
return [sum, sum1]
}
let x = ayy([5, 8, 9, 2, 1, 5])
// 和
console.log(x[0]);
// 平均值
console.log(x[1]);
function arr(ayy) {
let sum = 0
for (let i = 0; i < ayy.length; i++) {
if (ayy[i] > 5) {
sum += ayy[i]
}
}
return sum
}
let a = arr([4, 9, 5, 20, 3, 11])
console.log(a);
function ayy(arr) {
let sum = 0
let average = 0
for (let i = 0; i < arr.length; i++) {
sum += arr[i]
}
average = sum / arr.length
return [sum, average]
}
let x = ayy([15, 19, 21, 33, 18, 24])
//年龄和
console.log(x[0]);
//平均年龄
console.log(x[1]);
function ayy(arr) {
for (let i = 0; i < arr.length; i++) {
// arr[i] % 2 != 0 ? console.log(arr[i]) : 0
if (arr[i] % 2 != 0) {
document.write(`${
arr[i]}
`)
}
}
}
ayy([5, 2, 8, 10, 3, 7])
function ayy(arr) {
let sum = 0
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0 && arr[i] % 2 == 0) {
sum += arr[i]
}
}
return sum
}
let x = ayy([2, 6, 18, 15, 40])
console.log(x);
function ayy(arr) {
let sum = 0
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0 && arr[i] % 2 == 0) {
++sum
}
}
return sum
}
let x = ayy([2, 6, 18, 15, 40])
console.log(x);
let arr = [0, 44, 2435, 0, 23, 0, 4235]
// 创建新的数组用来接收
let arr1 = []
for (let i = 0; i < arr.length; i++) {
if (arr[i] + 1 != 1) {
arr1.push(arr[i])
}
}
console.log(arr1);
let arr = ['老赵', '老李', '老l', '小川']
let random = Math.floor(Math.random() * (arr.length))
document.write(arr[random])
arr.splice(random, 1)
console.log(arr);
-------------------------------------------------------------------------------
let a = [5, 2, 10, 4, 1, 6];
// for (let i = 0; i < a.length; i++) {
// if (a[i] === 10) {
// console.log('此数组包含10');
// }
// }
//includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
console.log(a.includes(10));
let b = [
{
id: 10, name: 'zs' },
{
id: 9, name: 'ls' },
{
id: 5, name: 'ww' },
{
id: 4, name: 'zl' },
{
id: 1, name: 'cq' },
]
for (let i = 0; i < b.length; i++) {
if (b[i].id == 4) {
console.log('包含');
console.log(b[i].id);
}
}
let b = [
{
id: 10, name: 'zs' },
{
id: 9, name: 'ls' },
{
id: 5, name: 'ww' },
{
id: 4, name: 'zl' },
{
id: 1, name: 'cq' },
]
// let obj = []
// for (let i = 0; i < b.length; i++) {
// if (b[i].id > 5) {
// obj.push(b[i].id)
// }
// }
// console.log(obj);
// console.log(b.filter(item=>item.id > 5));
//以下使用function函数方法,其主要作用是参数传递
var c = b.filter(function (item) {
return item.id > 5;
})
console.log(c);
let b = [
{
id: 10, name: 'zs' },
{
id: 9, name: 'ls' },
{
id: 5, name: 'ww' },
{
id: 4, name: 'zl' },
{
id: 1, name: 'cq' },
]
let int = []
for (let i = 0; i < b.length; i++) {
int.push({
name:b[i].name})
}
console.log(int);