为什么我的函数返回一个未定义为项的数组?

时间:2017-11-21 12:13:17

标签: javascript arrays

我试图创建一个接受数组的函数,然后返回带有随机项的该数组的副本,但是它会一直返回一个至少有一个索引为undefined -why的数组?



function randomize(array) {
  let newArr = array.slice(); //creates copy of array

  let arLength = newArr.length; //gets array length
  let newCollection = []; //creates a new array to restore items into

  let numList = []; //creates a new array to document the iterated random items

  function genRandNum() { //generates a random number and if the number already exists inside of numList it runs again

    let randNum = Math.floor(Math.random() * arLength);
    if (numList.includes(randNum)) {
      genRandNum();
    }
    return randNum;
  }

  for (let i = 0; i < arLength; i++) {
    let randomNum = genRandNum(); //gets random number

    numList.push(numList); //adds random number to random number list

    let item = newArr.splice(randomNum, 1)[0]; //gets index value of the array past in

    newCollection.push(item); //adds randomly pick item to the new array
  }
  return newCollection; //returns the new randomized array
}
console.log(randomize(["a","b","c"]));
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

有一些用于随机化数组的JavaScript函数,您可以轻松找到它们。我的目的不是改进您的代码或提出这些替代功能......而不是那样,我只是指出代码中的问题。

你有两个问题:

  1. 您想将randomNum推送到numList

    numList.push(randomNum);
    
  2. splice更改数组的长度。因此,newArr的长度每次let item = newArr.splice(randomNum, 1)[0];时都会减少,并且不再等于arrLength

  3. 因此,您可以使用while代替,将数组的长度传递给genRandNum。这背后的逻辑是,每次执行splice时,数组的长度都会减少。

    以下是包含这些更改的代码:

    &#13;
    &#13;
    function randomize(array) {
      let newArr = array.slice(); //creates copy of array
    
      let newCollection = []; //creates a new array to restore items into
    
      let numList = []; //creates a new array to document the iterated random items
    
      function genRandNum(arLength) { //generates a random number and if the number already exists inside of numList it runs again
    
        let randNum = Math.floor(Math.random() * arLength);
        if (numList.includes(randNum)) {
          genRandNum();
        }
        return randNum;
      }
    
      while (newArr.length) {
        let randomNum = genRandNum(newArr.length); //gets random number
    
        numList.push(randomNum); //adds random number to random number list
    
        let item = newArr.splice(randomNum, 1)[0]; //gets index value of the array past in
    
        newCollection.push(item); //adds randomly pick item to the new array
      }
      return newCollection; //returns the new randomized array
    }
    console.log(randomize(["a","b","c"]));
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

至少部分问题是genRandnum并不总是返回未使用的索引;它会对此进行检查,并递归计算一个新数字,但无论如何都要返回原始数据。

答案 2 :(得分:0)

function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
}

var myArray = ["a","b","c"];
shuffle(myArray);
console.log(myArray);

试试此代码

答案 3 :(得分:0)

function random(arr) {  
  var new_arr = [];

  for (var i = 0; i < arr.length; i++) {
    new_arr.splice(getRandom(0, arr.length), 0, arr[i]);
  }

  function getRandom(min, max) {
    return Math.random() * (max - min) + min;
  }

  return new_arr;
}

var new_array = random(['a', 'b', 'c']);

console.log(new_array);

答案 4 :(得分:0)

您没有在第26行定义newVideoCollection数组。

并且在第22行将数组再次推送到同一个数组是不正确的,如果正确理解你需要随机化数组但是非常不干净。