Study Notes

Study Notes

  • Source
  • WebDesign
  • Javascript

›Basic Algorithm Script

Basic Javascript

  • Basic JavaScript Overview

ES6

  • ES6 Overview

Regular expression

  • Regular Expression Overview
  • Lookahead
  • Capture Group

Debugging

  • Debugging Overview

Object Oriented Programming

  • Object Oriented Programming Overview
  • Mixin
  • Closure
  • IIFE

Functional Programming

  • Functional Programming

Basic Algorithm Script

  • Reverse a String
  • Factorialize a Number
  • Find the Longest Word in a String
  • Return Largest Numbers In Arrays
  • Repeat a String
  • Truncate a String
  • Finders Keepers
  • Boo Who
  • Title Case a Sentence
  • Slice and Splice
  • Falsy Bouncer
  • javascript-algorithms-anddata-structures/basic-algorithm-scripting/where-do-i-belong
  • Mutations
  • Chunky Monkey

Intermediate Algorithm Script

  • Sum All Numbers in a Range
  • Diff Two Arrays
  • Seek and Destroy
  • Wherefor Art Thou
  • Spinal Tap Case
  • Search and Replace
  • DNA Pairing
  • Missing letters
  • Sorted Union
  • Convert HTML Entities
  • Sum All Odd Fibonacci Numbers
  • Smallest Common Multiple
  • Drop it
  • Steamroller
  • Binary Agents
  • Everything Be True
  • Arguments Optional
  • Make a Person
  • Map the Debris

Return Largest Numbers In Arrays

个人解题思路:

  • 遍历最外层的数组
  • 获取第二层数组中的最大值
  • 保存第二层数组中的最大值

具体到代码:

  • map() 遍历了外层的数组(利用其每轮 return 的数据将组成新的数组的特性)
  • Math.max() 获取了第二层数组中最大的数
  • reduce() 遍历了第二层数组(主要是运用它的累加器特性,即 x 保留的是上一轮循环 return 的结构)
function largestOfFour(arr) {
  return arr.map(element => element.reduce((x, y) => Math.max(x, y)))
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

还可以利用 ES6 扩展运算符,连 reduce 都省了,使算法更为简洁。

function largestOfFour(arr) {
  return arr.map(element => Math.max(...element))
}
← Find the Longest Word in a StringRepeat a String →
Copyright © 2019 罗惠东