Q:

Write a JavaScript program to check whether a given array of integers represents either a strictly increasing or a strictly decreasing sequence

0

Write a JavaScript program to check whether a given array of integers represents either a strictly increasing or a strictly decreasing sequence.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function is_monotonous(num) {
    if (num.length === 1) {
        return true;
    }
    var num_direction = num[1] - num[0];
    for (var 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]))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now