Chunky Monkey

  1. Basic Algorithm Scripting: Chunky Monkey | freeCodeCamp.org
  2. freeCodeCamp Challenge Guide: Chunky Monkey - Guide - The freeCodeCamp Forum

第一版:

function chunkArrayInGroups(arr, size) {
  let newArr = []
  let subArr = []
  if (size >= arr.length) {
    return (newArr = arr)
  } else {
    arr.forEach(function (elem) {
      console.log(elem)
      subArr.push(elem)
      if (subArr.length === size) {
        newArr.push(subArr)
      }
    })
    return newArr
  }
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 5))

其他实现:

function chunkArrayInGroups(arr, size) {
  let newArr = []
  let subArr = []

  for (let a = 0; a < arr.length; a++) {
    if (a % size !== size - 1) subArr.push(arr[a])
    else {
      subArr.push(arr[a])
      newArr.push(subArr)
      subArr = []
    }
  }

  if (subArr.length !== 0) newArr.push(subArr)
  return newArr
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4))
function chunkArrayInGroups(arr, size) {
  let newArr = []

  for (let a = 0; a < arr.length; a += size) {
    newArr.push(arr.slice(a, a + size))
  }
  return newArr
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4))
function chunkArrayInGroups(arr, size) {
  let newArr = []
  let i = 0

  while (i < arr.length) {
    newArr.push(arr.slice(i, i + size))
    i += size
  }
  return newArr
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4))

这段代码不太理解

function chunkArrayInGroups(arr, size) {
  let newArr = []
  while (arr.length > 0) {
    newArr.push(arr.splice(0, size))
  }
  return newArr
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 3))

这段代码竟然用到了递归

function chunkArrayInGroups(arr, size) {
  if (arr.length <= size) return [arr]
  else {
    return [arr.slice(0, size)].concat(
      chunkArrayInGroups(arr.slice(size), size),
    )
  }
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"