0

How can I push the value return of anonymous function into the array. I have tried with this code but it not work. Where was I wrong

for (var i = 0; i < num; i++) {
        //get ramdom color
        // arr.push(randomColor());
        arr.push(function() {
            //pick a "red" from 0 - 255
            var r = Math.floor(Math.random() * 256);
            //green 0 - 255
            var g = Math.floor(Math.random() * 256);
            //blue 0 -255
            var b = Math.floor(Math.random() * 256);

            return "rgb" + "(" + r + ", " + g + ", " + b + ")";
        });
    }
2
  • Why not arr.push(randomColor());? Commented Jul 17, 2017 at 15:45
  • I have written randomcolor() already but I just want to try Anonymous function. Thank you. Commented Jul 17, 2017 at 15:58

3 Answers 3

1

Here's how you could do it:

for (var i = 0; i < num; i++) {
        //get ramdom color
        // arr.push(randomColor());
        arr.push(function() {
            //pick a "red" from 0 - 255
            var r = Math.floor(Math.random() * 256);
            //green 0 - 255
            var g = Math.floor(Math.random() * 256);
            //blue 0 -255
            var b = Math.floor(Math.random() * 256);

            return "rgb" + "(" + r + ", " + g + ", " + b + ")";
        }()); // Note the two brackets.
    }

Note the two brackets in second last line. They are meant to execute the function right away.

Be careful with such a pattern though. It is easy to confuse the reader with such code. You could rather use something such as:

function getRandomColor() {
    //pick a "red" from 0 - 255
    var r = Math.floor(Math.random() * 256);
    //green 0 - 255
    var g = Math.floor(Math.random() * 256);
    //blue 0 -255
    var b = Math.floor(Math.random() * 256);

    return "rgb" + "(" + r + ", " + g + ", " + b + ")";
}

for (var i = 0; i < num; i++) {
    //get ramdom color
    // arr.push(randomColor());
    arr.push(getRandomColor());
}
Sign up to request clarification or add additional context in comments.

2 Comments

I already have written the function getRandomColor but I just want to try it with an anonymous function.
Alright. Then the first sample does the job for you.
1

No need to complicate this:

for (var i = 0; i < num; i++) {
    //get ramdom color
    //pick a "red" from 0 - 255
    var r = Math.floor(Math.random() * 256);
    //green 0 - 255
    var g = Math.floor(Math.random() * 256);
    //blue 0 -255
    var b = Math.floor(Math.random() * 256);
    var colour = "rgb" + "(" + r + ", " + g + ", " + b + ")";
    arr.push(colour)
}

Comments

0

You didn't call these functions inside the loop.

let arr = [];
let num = 3;

for (let i = 0; i < num; i++) {
  arr.push((() => {
    let r = Math.floor(Math.random() * 256);
    let g = Math.floor(Math.random() * 256);
    let b = Math.floor(Math.random() * 256);

    return "rgb" + "(" + r + ", " + g + ", " + b + ")";
  })());
}

console.log(arr);

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.