Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Determine if array is in order AND the last element is 0

how can I determine when the first X numbers of an array is in order AND the last element in 0? i.e the array is

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0

I currently have this, but this relies on the array always being the same, which isn't very flexibile

const sorted = (array) => {
    const solved = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
    return (JSON.stringify(array) == JSON.stringify(solved))
}

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

First define a generic function to verify that a segment of an array is sorted, then define a second function that uses the first to see the first values are sorted, and add a check for the final value:

function isSegmentSorted(array, start=0, end=array.length) {
    for (let i = start + 1; i < end; i++) {
        if (array[i - 1] > array[i]) return false; 
    }
    return true;
}

function isSortedWithExtraZero(array) {
    return array.at(-1) === 0 &&
           isSegmentSorted(array, 0, array.length - 1);
}

var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0];
console.log(isSortedWithExtraZero(array));

about 4 years ago · Santiago Gelvez Report

0

You could do something as simple as this:

const checkArray = (arr) => {
    if(arr[arr.length-1] != 0){
        return false;
    }
    const nums = arr.slice(0, arr.length - 1);
    const sortedArr = [...nums].sort((a, b) => a - b);
    for (let i = 0; i < nums.length; i++) {
      if(nums[i] != sortedArr[i]){
        return false;
      }
    }
    return true;
}

console.log(checkArray([1,2,3,0])); // true
console.log(checkArray([1,2,3,4])); // false
console.log(checkArray([1,3,2,0])); // false

Basically the steps are:

  • Check if last element is 0, else do an early return.
  • Create a sorted version of the first part of the array (the one with the numbers)
  • Check if the numbers part is equal to the sorted array. If any element is different return false

In the end you return true only if every condition is verified.

This is generic enough so that if in the future you want to change the sorting type you can just act on the sort function (for example if you want to make it descending).

about 4 years ago · Santiago Gelvez Report

0

We can use every function for that. With !idx we exclude the first index 0, then we check if idx is less than the length of your array. If so, check if it is sorted, else check it it equals to zero.

const solutions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0];

const sorted = solutions.every((val, idx, arr) =>
  !idx || (idx < solutions.length - 1 ? arr[idx - 1] <= val : val === 0)
);

console.log(sorted);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!