vljs
vljs

Reputation: 1030

return multiple numbers from the loop

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));
    }
}

Upvotes: 1

Views: 2790

Answers (6)

Robbie Milejczak
Robbie Milejczak

Reputation: 5770

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.

Upvotes: 3

VHS
VHS

Reputation: 10184

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());

Upvotes: 2

Jonas Wilms
Jonas Wilms

Reputation: 138257

That can also be done with some cool ES6 tricks:

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

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386560

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; }

Upvotes: 2

Dij
Dij

Reputation: 9808

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;
}

Upvotes: 2

kip
kip

Reputation: 1140

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;
}

Upvotes: 2

Related Questions