Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda