Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
The some() and every() methods operate on arrays (including arrays of objects).
The some() method will return true if at least one item in the array matches a given condition.
some() applied to an array (Run Example)
<!DOCTYPE html> <html> <head> <title>Example using some() method</title> <meta http-equiv="Content-Type" content="text/htmlcharset=utf-8"> <meta http-equiv="Content-Language" content="en" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> let numbers = [1, 3, 6, 8, 11] let arrayContainsNumber = numbers.some(number => number === 6) console.log(`arrayContainsNumber is ${arrayContainsNumber}`) // will display true, as the number 6 occurs at least once in the array </script> </head> <body> <div>View the console output in browser's Web DevTools (F12)</div> </body> </html>
Write code to use a for-loop instead of the some() method, as shown here.
The every() method will return true if all of the items in the array matches a given condition.
every() applied to an array (Run Example)
<!DOCTYPE html> <html> <head> <title>Example using every() method</title> <meta http-equiv="Content-Type" content="text/htmlcharset=utf-8"> <meta http-equiv="Content-Language" content="en" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> let numbers = [1, 3, 6, 8, 11] let arrayContainsNumber = numbers.every(number => number === 6) console.log(`arrayContainsNumber is ${arrayContainsNumber}`) // will display false, as the number 6 occurs only least once in the array </script> </head> <body> <div>View the console output in browser's Web DevTools (F12)</div> </body> </html>
Write code to use a for-loop instead of the every() method, as shown here.
Write code that uses the every() method to test if all the numbers in an array are even, as shown here.
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.