- Published on
How do I check whether or not an array of numbers are in consecutive order using JavaScript?
const inOrder = [3, 4, 5, 6, 7, 8];const outOfOrder = [14, 34, 2, 3, 10, 18];
function checkIfInOrder(numbers) { return !!numbers.reduce((accumulator, currentValue) => (accumulator ? (accumulator + 1 === currentValue ? currentValue : false) : false));}
console.log(checkIfInOrder(inOrder)); // trueconsole.log(checkIfInOrder(outOfOrder)); // false