Flags in JavaScript functions

You can implicitly use flags in functions using the return statement. Let's see how it's done. Suppose we have the following function that checks that all array elements are positive numbers:

function isPositive(arr) { let flag = true; for (let elem of arr) { if (elem < 0) { flag = false; } } return flag; }

Let's rewrite the function code with implicit flags:

function isPositive(arr) { for (let elem of arr) { if (elem < 0) { return false; } } return true; }

How it works: if there is a required element in the array, we exit the function (and the loop too) with return. But if the required element is not found in the array, the function will not exit and execution will reach the command return true. And it turns out that the function will return true as a sign that all elements in the array are positive.

Make a function that takes an array of numbers as a parameter, and check that all elements in this array are even numbers.

Make a function that will take a number as a parameter and check that all digits of this number are odd.

Make a function that takes an array as a parameter and checks if there are two identical elements in a row in this array.

enru