w3resource

JavaScript: Check whether a given array of integers represents either a strictly increasing or a strictly decreasing sequence

JavaScript Basic: Exercise-122 with Solution

Check if Array is Strictly Increasing/Decreasing

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

Visual Presentation:

JavaScript: Check whether a given array of integers represents either a strictly increasing or a strictly decreasing sequence.

Sample Solution:

JavaScript Code:

// Function to check if a sequence of numbers is monotonous
function is_monotonous(num) {
    // If the sequence has only one element, it's considered monotonous
    if (num.length === 1) {
        return true;
    }

    // Calculate the direction of the sequence
    var num_direction = num[1] - num[0];

    // Check for non-monotonic behavior
    for (var i = 0; i < num.length - 1; i++) {
        // If the product of direction and the difference between elements is not positive, it's non-monotonic
        if (num_direction * (num[i + 1] - num[i]) <= 0) {
            return false;
        }
    }
    // If the loop completes, the sequence is monotonic
    return true;
}

// Test cases
console.log(is_monotonous([1, 2, 3]));    // Output: true (Monotonously increasing)
console.log(is_monotonous([1, 2, 2]));    // Output: false (Not strictly increasing)
console.log(is_monotonous([-3, -2, -1])); // Output: true (Monotonously increasing) 

Output:

true
false
true

Live Demo:

See the Pen javascript-basic-exercise-122 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Check whether a given array of integers represents either a strictly increasing or a strictly decreasing sequence

ES6 Version:

// Function to check if a sequence of numbers is monotonous
const is_monotonous = (num) => {
    // If the sequence has only one element, it's considered monotonous
    if (num.length === 1) {
        return true;
    }

    // Calculate the direction of the sequence
    const num_direction = num[1] - num[0];

    // Check for non-monotonic behavior
    for (let i = 0; i < num.length - 1; i++) {
        // If the product of direction and the difference between elements is not positive, it's non-monotonic
        if (num_direction * (num[i + 1] - num[i]) <= 0) {
            return false;
        }
    }
    // If the loop completes, the sequence is monotonic
    return true;
};

// Test cases
console.log(is_monotonous([1, 2, 3]));    // Output: true (Monotonously increasing)
console.log(is_monotonous([1, 2, 2]));    // Output: false (Not strictly increasing)
console.log(is_monotonous([-3, -2, -1])); // Output: true (Monotonously increasing)

For more Practice: Solve these Related Problems:

  • Write a JavaScript program that checks if an array of integers is strictly increasing by comparing each element with its predecessor.
  • Write a JavaScript function that determines whether an array is strictly increasing or strictly decreasing without altering the original order.
  • Write a JavaScript program that returns a descriptive message indicating if an array is strictly increasing, strictly decreasing, or neither.

Go to:


PREV : Check if Matrix is Lower Triangular.
NEXT : Check if Array is Permutation of Numbers 1 to n.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.