
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the inclination of arrays in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns true if it’s either strictly increasing or strictly decreasing, otherwise returns false.
In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657]; const sameSlope = (a, b, c) => (b - a < 0 && c - b < 0) || (b - a > 0 && c - b > 0); const increasingOrDecreasing = (arr = []) => { if(arr.length <= 2){ return true; }; for(let i = 1; i < arr.length - 1; i++){ if(sameSlope(arr[i-1], arr[i], arr[i+1])){ continue; }; return false; }; return true; }; console.log(increasingOrDecreasing(arr)); console.log(increasingOrDecreasing(arr2));
Output
The output in the console will be −
false true
- Related Articles
- Finding the continuity of two arrays in JavaScript
- Finding the intersection of arrays of strings - JavaScript
- Finding intersection of multiple arrays - JavaScript
- Finding intersection of arrays of intervals in JavaScript
- Finding reversed index of elements in arrays - JavaScript
- Finding the difference between two arrays - JavaScript
- Finding the common streak in two arrays in JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- Finding content of arrays on the basis of specific property in JavaScript?
- Finding deviations in two Number arrays in JavaScript
- Finding Common Item Between Arbitrary Number of Arrays in JavaScript
- Finding intersection of arrays that contain repetitive entries in JavaScript
- Finding maximum number from two arrays in JavaScript
- Finding the sum of all common elements within arrays using JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript

Advertisements