Answers for "strictly increasing or strictly decreasing javascript"

0

strictly increasing or strictly decreasing javascript

function is_monotonous(num) {
    if (num.length === 1) {
        return true;
    }
    const num_direction = num[1] - num[0];
    for (let i = 0; i < num.length - 1; i++) {
        if (num_direction * (num[i + 1] - num[i]) <= 0) {
            return false;
        }
    }
    return true;
}
console.log(is_monotonous([1, 2, 3]));
console.log(is_monotonous([1, 2, 2]))
console.log(is_monotonous([-3, -2, -1]))
Posted by: Guest on July-15-2020

Code answers related to "strictly increasing or strictly decreasing javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language