数组测试题

把数字变成星期

let arr = [0,1,2,2,3,3,3,4,4,4,4,6]
let arr2 = arr.map(补全代码)
console.log(arr2)
// ['周日', '周一', '周二', '周二', '周三', '周三', '周三', '周四', '周四', '周四', '周四','周六']

请使用 arr.map 把 0 变成'周日',把 1 变成'周一',以此类推

let arr = [0,1,2,2,3,3,3,4,4,4,4,6]
let arr2 = arr.map(day => {
    switch(day){
    case 0:
      return '周日';
      break;
    case 1:
      return '周一';
      break;
    case 2:
      return '周二';
      break;
    case 3:
      return '周三'; 
      break;
    case 4:
      return '周四';
      break;
    case 5:
      return '周五';
      break;
    case 6:
      return '周六';
    default:
      
  }
});
console.log(arr2) // ['周日', '周一', '周二', '周二', '周三', '周三', '周三', '周四', '周四', '周四', '周四','周六']

数组测试题

找出所有大于 60 分的成绩

let scores = [95,91,59,55,42,82,72,85,67,66,55,91]
let scores2 = scores.filter(补全代码)
console.log(scores2) // [95,91,82,72,85,67,66, 91]

let scores = [95,91,59,55,42,82,72,85,67,66,55,91]
let scores2 = scores.filter(grade => grade > 60)
console.log(scores2) //  [95,91,82,72,85,67,66, 91]

数组测试题

算出所有奇数之和

let scores = [95,91,59,55,42,82,72,85,67,66,55,91]
let sum = scores.reduce((sum, n)=>{
补全代码
},0)
console.log(sum) // 奇数之和:598

let scores = [95,91,59,55,42,82,72,85,67,66,55,91]
let sum = scores.reduce((sum,n)=>{
    if(n % 2 !== 0){
    sum = sum + n
  }
  return sum
},0)
console.log(sum) // 奇数之和:598 

数组测试题

上一篇:白白的python之路--Day5


下一篇:Java基础案例集