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

190
Visualizações
Function that takes an array and returns elements inside rotated n spaces in js

Create a function named "rotate" that takes an array and returns a new one with the elements inside rotated n spaces.

If n is greater than 0 it should rotate the array to the right. If n is less than 0 it should rotate the array to the left. If n is 0, then it should return the array unchanged.

Example:

var data = [1, 2, 3, 4, 5];

rotate(data, 1) // => [5, 1, 2, 3, 4]
rotate(data, 2) // => [4, 5, 1, 2, 3]
rotate(data, 3) // => [3, 4, 5, 1, 2]
rotate(data, 4) // => [2, 3, 4, 5, 1]
rotate(data, 5) // => [1, 2, 3, 4, 5]

rotate(data, 0) // => [1, 2, 3, 4, 5]

rotate(data, -1) // => [2, 3, 4, 5, 1]
rotate(data, -2) // => [3, 4, 5, 1, 2]
rotate(data, -3) // => [4, 5, 1, 2, 3]
rotate(data, -4) // => [5, 1, 2, 3, 4]
rotate(data, -5) // => [1, 2, 3, 4, 5]

Furthermore the method should take ANY array of objects and perform this operation on them:

rotate(['a', 'b', 'c'], 1)     // => ['c', 'a', 'b']
rotate([1.0, 2.0, 3.0], 1)     // => [3.0, 1.0, 2.0]
rotate([true, true, false], 1) // => [false, true, true]

Finally, the rotation shouldn't be limited by the indices available in the array. Meaning that if we exceed the indices of the array it keeps rotating.

Example:

var data = [1, 2, 3, 4, 5]
rotate(data, 7)     // => [4, 5, 1, 2, 3]
rotate(data, 11)    // => [5, 1, 2, 3, 4]
rotate(data, 12478) // => [3, 4, 5, 1, 2]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could basically do this with a while loop and increment or decrement based on the number parameter, if its positive or negative.

function rotate(data, n) {
  const output = [...data]

  if (n === 0) {
    return data
  }

  const neg = n < 0
  const last = output.length - 1
  const toIndex = neg ? last : 0
  const index = neg ? 0 : last

  while ((neg ? n++ : n--) !== 0) {
    output.splice(toIndex, 0, output.splice(index, 1)[0])
  }

  return output
}


var data = [1, 2, 3, 4, 5];

console.log(rotate(data, 2)) // => [4, 5, 1, 2, 3]
console.log(rotate(data, 3)) // => [3, 4, 5, 1, 2]
console.log(rotate(data, 4)) // => [2, 3, 4, 5, 1]
console.log(rotate(data, 5)) // => [1, 2, 3, 4, 5]


console.log(rotate(data, -1)) // => [2, 3, 4, 5, 1]
console.log(rotate(data, -2)) // => [3, 4, 5, 1, 2]
console.log(rotate(data, -3)) // => [4, 5, 1, 2, 3]
console.log(rotate(data, -4)) // => [5, 1, 2, 3, 4]
console.log(rotate(data, -5)) // => [1, 2, 3, 4, 5]

console.log(rotate(['a', 'b', 'c'], 1))
console.log(rotate([true, true, false], 1))

about 4 years ago · Juan Pablo Isaza Relatório

0

function rotate(array,n){
  let len = array.length;
  n=Math.abs(n)>len?n%len:n
   if(n>0){
    let firstPart=array.slice(len-n,len);
    let lastPart=array.slice(0,len-n);
    console.log(firstPart,lastPart)
    return [...firstPart,...lastPart]
   }else{
    let firstPart=array.slice(0,n);
    let lastPart=array.slice(n,len);
    console.log(firstPart,lastPart)
    return [...lastPart,...firstPart]
   }
}
    console.log(rotate([1, 2, 3, 4, 5],1));

This code passes almost all tests but some tests cannot be passed. So can anyone help me with this?

about 4 years ago · Juan Pablo Isaza 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