admin管理员组

文章数量:1320661

I want to create a function that returns 50 random numbers out of 100. If I write console.log(getMines), it returns all fifty numbers. But if I want to actually use them, my loop returns only one number. What's the problem?

var getMines = function() {
    for (count; count <= 100; count++) {
        return "d" + (Math.floor(Math.random() * 50));
    }
}

I want to create a function that returns 50 random numbers out of 100. If I write console.log(getMines), it returns all fifty numbers. But if I want to actually use them, my loop returns only one number. What's the problem?

var getMines = function() {
    for (count; count <= 100; count++) {
        return "d" + (Math.floor(Math.random() * 50));
    }
}
Share Improve this question asked Sep 18, 2017 at 16:13 vljsvljs 1,0309 silver badges16 bronze badges 3
  • 4 The "problem" is, that return stops the execution of the current function, totally. – Teemu Commented Sep 18, 2017 at 16:15
  • 1 You should use an array and push the random numbers into that array – drew_w Commented Sep 18, 2017 at 16:15
  • 1 "my loop returns only one number".. Looks like your question has the answer ;) – maazadeeb Commented Sep 18, 2017 at 16:16
Add a ment  | 

6 Answers 6

Reset to default 3

You can only return a single value from a function. So once you return your first number, the loop essentially ends.

Try this instead!

var getMines = function() {
    var minesArray = [];
    for (var count=0; count <= 100; count++) {
        minesArray.push("d" + (Math.floor(Math.random() * 50)));
    }
    return minesArray;
}

This will return an array of all the numbers, which you can then iterate through to do what you need. You can sort the numbers, or sum them etc.

I feel I should point out that your code is returning 100 random numbers between 1 and 50, not 50 random numbers between 1 and 100. If thats what you want and you misspoke in the OP nbd, but if you want 50 random numbers between 1 and 100 change count to <= 50 and multiply Math.random() by 100 instead.

You need create a var and push randoms, like this

var getMines = function() {
    var returnArray = [];
    for (var count = 0; count <= 100; count++) {
        returnArray.push("d" + (Math.floor(Math.random() * 50)));
    }
    return returnArray;
}

you can only return only once from a function, you can use an array or object, add all values to it and return it.

var getMines = function() {
    var arr = [];
    for (count; count <= 100; count++) {
        arr.push("d" + (Math.floor(Math.random() * 50)));
    }
  return arr;
}

If I read you question right, you need 50 numbers with a random value out of 100.

You need to switch the numbers in the for loop and the factor for getting a random number and take a initial value of zero for the looping variable, as well as not return some value inside of the for loop.

var getMines = function() {
    var count,
        array = [];

    for (count = 0; count < 50; count++) {
        array.push("d" + (Math.floor(Math.random() * 100)));
    }

    return array;
}

console.log(getMines());
.as-console-wrapper { max-height: 100% !important; top: 0; }

That can also be done with some cool ES6 tricks:

const getMines = ()=> Array.from(
   {length:50},
   ()=> "d" + Math.floor( Math.random() * 100 )
);

If you need any random 50 numbers between 1 and 100 (both inclusive, I have tweaked your version to following. See if that helps.

 var getMines = function() {
        var nums = [];
        for (var count=1; count <= 50; count++) {
            nums[count-1]=Math.floor(Math.random() * 100) + 1;
        }
        return nums;
    }
    console.log(getMines());

本文标签: javascriptreturn multiple numbers from the loopStack Overflow