Guest User

Untitled

a guest
Oct 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. const flattenArray = (array = []) => {
  2. let queue = [...array]
  3. let output = []
  4.  
  5. while (queue.length) {
  6. const element = queue.shift()
  7.  
  8. if (Number.isInteger(element)) {
  9. output.push(element)
  10. } else if (Array.isArray(element)) {
  11. queue.unshift(...element)
  12. } else if (!Number.isNaN(element % 1)) {
  13. // typeof would report the generic 'number', so create a special case to clarify
  14. // modulus 1 returns NaN for non-numbers, and we know element is not an integer
  15. throw new Error(`Undefined behavior for element of type: (non-integer) number`)
  16. } else {
  17. throw new Error(`Undefined behavior for element of type: ${typeof element}`)
  18. }
  19. }
  20.  
  21. return output
  22. }
  23.  
  24. module.exports = flattenArray
Add Comment
Please, Sign In to add comment