0

I have the following code:

var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'];
var otherArray = [];


function takeOut() {
    for ( i = 0; i < 3; i++ ) {
        var randItem = disArray[Math.floor(Math.random()*disArray.length)];
        otherArray.push(randItem);
    }
    return otherArray;
}

takeOut(disArray)
console.log(otherArray)

I want the function to return the elements in otherArray when it is called, but it I get the error undefined. It only works when I console.log otherArray. Is there any way that I can make the function return the array without using console.log?

4
  • 1
    otherArray = takeOut(disArray); console.log(otherArray); Commented Aug 17, 2016 at 15:36
  • 1
    otherArray is not in the scope of the function. Commented Aug 17, 2016 at 15:36
  • undefined is not an error. You don’t even get any error. Commented Aug 17, 2016 at 15:39
  • Your function returns the array regardless of whether you call console.log. I'm not able to reproduce the undefined value. Just do console.log(takeOut(disArray)) Commented Aug 17, 2016 at 15:41

2 Answers 2

1

You could use a local variable.

function takeOut() {
    var otherArray = [], i, randItem;
    for (i = 0; i < 3; i++ ) {
        randItem = disArray[Math.floor(Math.random() * disArray.length)];
        otherArray.push(randItem);
    }
    return otherArray;
}

var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'],
    result = takeOut(disArray);

console.log(result);

For a reusable function, you could add some parameters to the function, like the array and the count, you need.

function takeOut(array, count) {
    var result = [];
    while (count--) {
        result.push(array[Math.floor(Math.random() * array.length)]);
    }
    return result;
}

var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'],
    result = takeOut(disArray, 5);

console.log(result);

Example for calling takeOut multiple times and store the result in an array.

function takeOut(array, count) {
    var result = [];
    while (count--) {
        result.push(array[Math.floor(Math.random() * array.length)]);
    }
    return result;
}

var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'],
    i = 7,
    result = []

while (i--) {
    result.push(takeOut(disArray, 5));
}

console.log(result);

Sign up to request clarification or add additional context in comments.

11 Comments

More specifically, you are using the return value of takeOut() and passing it in to console.log.
right. the result set is independent from the former variable otherArray, now.
You should also add disArray as a parameter in the function declaration, so that it can use any array as a parameter and not just the global. Right now passing takeOut() a parameter does nothing.
Thank you for the help. Is there any way to use this functions multiple times for it to return different results ?
that it usually the idea behind functions -- reusable and with Math.random it should return for every call a different result.
|
0

Basically a call to takeOut() is returning a value using a return. If you want to print on the console, you need to pass it to the console.log() fn. The other way is to assign the fn call ie. takeOut() to a variable and direct the variable to the console or use elsewhere.

var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'];
var otherArray = [];


function takeOut() {
    for ( i = 0; i < 3; i++ ) {
        var randItem = disArray[Math.floor(Math.random()*disArray.length)];
        otherArray.push(randItem);
    }
    return otherArray;
}

takeOut()  //need to utilize the returned variable somewhere.
console.log(takeOut())  //prints to stackoverflow.com result // inspect browser console

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.