1. 请编写getMissingElement函数,返回给定数组中缺少的元素(数组里的元素为0~9,只会缺失一个)。
Example:
getMissingElement( [0, 5, 1, 3, 2, 9, 7, 6, 4] ) // returns 8
getMissingElement( [9, 2, 4, 5, 7, 0, 8, 6, 1] ) // returns 3
soluction:
function getMissingElement(superImportantArray){ return superImportantArray.reduce(function(sum,i){return sum - i;},45) }
2.Write a function that flattens an Array of Array objects into a flat Array. Your function must only do one level of flattening.
Exmaple:
flatten([123]) // => [1,2,3]
flatten([[1,2,3],["a","b","c"],[1,2,3]]) //=>[1,2,3,"a","b","c",1,2,3]
flatten([[[1,2,3]]]) // => [[1,2,3]]
soluction:
var flatten = function (array){ return array.reduce(function(a,b){ return a.concat(b); },[]) }
时间: 2024-10-01 04:22:01