From 84d25842188df6d9312e1c969a190bf9cf7212b5 Mon Sep 17 00:00:00 2001 From: Itamar Yatom Date: Thu, 8 Oct 2020 14:36:30 +0300 Subject: [PATCH] Update getElementsBiggerThanViewport.md There is no reason to use reduce when when you don't need to. `.filter()` is much simpler to reason, easier to read and more verbose --- snippets/getElementsBiggerThanViewport.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/snippets/getElementsBiggerThanViewport.md b/snippets/getElementsBiggerThanViewport.md index 664cbf985..1dab76f2f 100644 --- a/snippets/getElementsBiggerThanViewport.md +++ b/snippets/getElementsBiggerThanViewport.md @@ -6,15 +6,12 @@ tags: browser,intermediate Returns an array of HTML elements whose width is larger than that of the viewport's. - Use `HTMLElement.prototype.offsetWidth()` to get the width of the `document`. -- Use `Array.prototype.reduce()` on the result of `document.querySelectorAll()` to check the width of all elements in the document. +- Use `Array.prototype.filter()` on the result of `document.querySelectorAll()` to check the width of all elements in the document. ```js const getElementsBiggerThanViewport = () => { const docWidth = document.documentElement.offsetWidth; - return [...document.querySelectorAll('*')].reduce((acc, el) => { - if (el.offsetWidth > docWidth) acc.push(el); - return acc; - }, []); + return [...document.querySelectorAll('*')].filter(el => el.offsetWidth > docWidth); } ```