Skip to content

Случайная последовательность из массива #239

Discussion options

You must be logged in to vote

slice/keep варианты

function keepRandomSequence(array, count) {
  Combiner.replace(array, array.sliceRandomSequence(count))
}

function sliceRandomSequence(array, count) {
  return array.sliceRandomSequence(count)
}

Array.prototype.sliceRandomSequence = function (count) {
  let max = this.length - count + 1
  let startIndex = Math.floor(Math.random() * max);
  return this.slice(startIndex, startIndex + count);
}

Использование

function debug() {
  let x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  console.log(x.sliceRandomSequence(3))

  let tracks = Source.getSavedTracks(100)
  let randomSequence1 = tracks.sliceRandomSequence(10)

  keepRandomSequence(tracks, 10)
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Bondian
Comment options

Answer selected by Bondian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants