javascript - 勾选随机开始

标签 javascript arrays typescript underscore.js

我想创建一个整数的勾号旋转。

例如,我有数组中的数字列表:

let array = [
    1,2,3,4,5,6,7
]

然后找到一个随机开始。

例如

let random_start = 4

现在刻度结果应该是:

console.log(this.tickoff(array,random_start))

// 4,5,6,7,1,2,3

最佳答案

这工作正常。

function tickOff(array, random)
{
    var result = array.splice(random,array.length-random);
    return result.concat(array);
}

var array = [1,2,3,4,5,6,7];
var index = Math.floor(Math.random()*array.length), value = array[index];
console.log("Tick off with random index : "+index+" Result : "+tickOff(array, index).join(","));

var array = [1,2,3,4,5,6,7];
var value = Math.floor(Math.random()*array.length)+1, index = array.indexOf(value);
console.log("Tick off with random value : "+value+" Result : "+tickOff(array, index).join(","));

关于javascript - 勾选随机开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48515211/

相关文章:

c++ - 为什么指向字符串数组的指针会颠倒索引顺序?

javascript - 如何将 Map 定义为 Angular TypeScript 方法中的参数?

arrays - Typescript:如何声明字典的类型列表?

javascript - 如何将值推送到对象内的数组

node.js - 为 WebSocket 和 net.Socket 分配唯一 ID

javascript - 检查一个数组是否是另一个数组的子集(但要检查一个属性)

javascript - React - 如何获得样式化组件的顶部位置?

javascript - Bootstrap .container-fluid 溢出

javascript - JS : How to remove style tags and their content from an HTML string, 使用正则表达式?

ruby - 重写一个 "better"代码