Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda