相关疑难解决方法(0)

没有任何{}的for循环

所以我正在阅读关于改组阵列的内容.然后我遇到了这个脚本:

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};
Run Code Online (Sandbox Code Playgroud)

当我仔细观察时,for甚至根本没有{}!但它像魔术一样有效.我很想知道它是如何工作的.(还有一堆逗号.)

javascript

5
推荐指数
1
解决办法
5068
查看次数

生成唯一的 6 位代码 js

我正在尝试使用 JS 生成 6 位代码。

它必须包含 3 个数字和 3 个字符。

这是我的代码

var numbers = "0123456789";
var chars = "acdefhiklmnoqrstuvwxyz";
var string_length = 3;
var randomstring = '';
var randomstring2 = '';
for (var x = 0; x < string_length; x++) {
    var letterOrNumber = Math.floor(Math.random() * 2);
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum, rnum + 1);
}
for (var y = 0; y < string_length; y++) {
    var letterOrNumber2 = Math.floor(Math.random() * 2);
    var rnum2 = Math.floor(Math.random() …
Run Code Online (Sandbox Code Playgroud)

javascript

5
推荐指数
1
解决办法
5722
查看次数

简单的 for 循环似乎无法正常工作

我在不重复的情况下随机获取一个包含 1 到 16 数字的数组时遇到了麻烦。我制作了num数组来放置来自createNum函数的数字。

createNum函数具有一个for循环它得到从1到16的数字,直到if语句推16号写入NUM阵列。

最后,我运行 createNum() 并在网络上显示数字。当我制作这段代码时,它有时会起作用,但现在不起作用。

有人能指出我哪里出错了吗?

let num = [];

function createNum () {
    for (i = 0; i <= 15;) {
        let numGen = Math.floor(Math.random() * 15) + 1;
        if (!num.includes(numGen)) {
            num.push(numGen);
            i++;
        };
    };
}
console.log(createNum());
document.getElementById("selectedNumbersShownHere").innerHTML = num;
console.log(num);
Run Code Online (Sandbox Code Playgroud)

javascript loops push function

5
推荐指数
1
解决办法
135
查看次数

仅使用一次数组元素

我是JavaScript新手,我想从我的数组中获取三个随机元素,这些元素需要不同.我已经设法得到三个不同的元素,然后我尝试通过split方法只获取一个元素.但显然这不起作用,可能有很多错误,因为这是我的第一个脚本之一.它有时也说"未定义".

http://jsfiddle.net/t4mtpm50/

HTML:

<span id="tot1"></span>, <span id="tot2"> und </span> und <span id="tot3"></span>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

function getNumber() {
    random = Math.floor(Math.random() * students.length);
    students.splice(random,1);
    return random;
}

students = new Array("Paul", "Jan", "Fabian D.", "Fabian K.", "Lennard", 
                            "Robin", "Angelique", "Joyce", "Sarah", "Ajlin",
                            "Enes", "Leon", "Boran", "Joshua")    

getNumber();
tot1 = students[random];
getNumber();
tot2 = students[random];
getNumber();
tot3 = students[random];

document.getElementById('tot1').innerHTML = tot1;
document.getElementById('tot2').innerHTML = tot2;
document.getElementById('tot3').innerHTML = tot3;
Run Code Online (Sandbox Code Playgroud)

html javascript arrays random jquery

4
推荐指数
2
解决办法
1558
查看次数

Javascript - 生成一个没有重复数字的randome 4位数字

我有这个功能来创建一个随机的4位数字

    generateCode = function(){
      var fullNumber = [];
      var digit1 = Math.floor(Math.random() * 9) + 1;
      var digit2 = Math.floor(Math.random() * 9) + 1;
      var digit3 = Math.floor(Math.random() * 9) + 1;
      var digit4 = Math.floor(Math.random() * 9) + 1;
      fullNumber.push(digit1, digit2, digit3, digit4);
      this.theCode(fullNumber.join("")) ;
    }
Run Code Online (Sandbox Code Playgroud)

但我需要创建一个没有重复数字的4位数随机数.所以"1234"或"9673"......但不是"1145"或"5668"

任何帮助都是非常感谢的人!不确定有效的方式来实现这个目标

javascript random math

4
推荐指数
1
解决办法
2305
查看次数

How do you look through arrays more efficiently? Javascript

I've been doing a lot of the practise tests on codility and though all my solutions work, they always have hidden stress tests which include massive arrays, the following code for example needs to take array A and find the lowest positive number missing from the sequence.

e.g: given A = [1, 3, 6, 4, 1, 2], the function should return 5. 5 is the value of i which increments in the loop. Function returns whenever index i is not …

javascript arrays loops

4
推荐指数
1
解决办法
74
查看次数

以相同的方式打乱多个 javascript 数组

我有两个数组

var mp3 = ['sing.mp3','song.mp3','tune.mp3','jam.mp3',etc];
var ogg = ['sing.ogg','song.ogg','tune.ogg','jam.ogg',etc];
Run Code Online (Sandbox Code Playgroud)

我需要对两个数组进行洗牌,以便它们以相同的方式出现,例如:

var mp3 = ['tune.mp3','song.mp3','jam.mp3','sing.mp3',etc];
var ogg = ['tune.ogg','song.ogg','jam.ogg','sing.ogg',etc];
Run Code Online (Sandbox Code Playgroud)

stackoverflow 上有几篇文章以不同的方式对数组进行混洗——这个非常棒——但没有一篇文章展示了如何以完全相同的方式对两个数组进行混洗。

谢谢!

javascript arrays sorting shuffle

3
推荐指数
1
解决办法
3236
查看次数

如何合并和混洗数组数组

拥有数组数组

var a = [[[1, "alpha", "a"],
          [2, "beta",  "b"]],
         [[3, "gama",  "c"]],
         [[4, "delta", "d"]]];

var b = [];
Run Code Online (Sandbox Code Playgroud)

1)如何合并a[0]a[2]b

2)我怎样才能改组阵列b


这是我正在使用的随机算法>>

Array.prototype.shuffle = function() {
  for (var i = 0; i < this.length; i++)
    this.push(this.splice(Math.random() * (this.length - i), 1)[0]);
  return this;
}
Run Code Online (Sandbox Code Playgroud)

语法

myArray.shuffle();
Run Code Online (Sandbox Code Playgroud)

javascript arrays jquery

2
推荐指数
1
解决办法
934
查看次数

从数组中获取随机元素返回相同的元素

请参考以下代码.

for (var i = 0; i < elements.length; i++) 
{
     //var element = elements[Math.floor(Math.random()*elements.length)];
     this.animateSymbol(elements[Math.floor(Math.random()*elements.length)]);
}
Run Code Online (Sandbox Code Playgroud)

elements数组包含svg元素列表(circle/path/ellipse等).我想从elements数组中选择随机元素.

它在某些情况下返回相同的元素我想随机选择元素,不需要再次选择相同的元素.需要从该数组中选择不同的元素.

有什么问题 ?为什么它返回相同的索引和相同的元素?

谢谢,

湿婆

javascript random jquery

2
推荐指数
1
解决办法
1329
查看次数

随机播放jQuery选择

我试图改变jQuery选择的顺序,而不是洗涤DOM中的元素.

我需要随机选择以随机顺序为每个元素添加一个类,这就是我的代码应该是这样的:

$(".item").shuffle().each(function (i, element) {
    $(element).delay(i * 100).queue(function (next) {
        $(this).addClass("shown");
        next();
    });
});
Run Code Online (Sandbox Code Playgroud)

有人可以帮我找到shuffle()我需要的功能吗?

javascript jquery

2
推荐指数
1
解决办法
858
查看次数

标签 统计

javascript ×10

arrays ×4

jquery ×4

random ×3

loops ×2

function ×1

html ×1

math ×1

push ×1

shuffle ×1

sorting ×1