Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:42:31+00:00 2026-05-22T02:42:31+00:00

Im sure this is another time I’m over complicating things, or just making myself

  • 0

Im sure this is another time I’m over complicating things, or just making myself loopy.

With just vanilla javascript, i want to make this code output an array of every rgb color code, (im using 25 instead of 255 just for sanity)

var a = [];
    for( var i = 0; i < 25; i++ ) {
for( var j = 0; j < 4; j++ ) {
     a.push(i)
      console.log('rgb('+a+')');
    }
}

the above outputs this 25 times:
rgb(0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6…. so on …25,25,25)

Then i tried this, or something like this, i lost what i had planned to show as example i think lol.

var a = [];
for( var j = 0; j < 4; j++ ) {
    for( var i = 0; i < 25; i++ ) {
     a.push(i)
      console.log('rgb('+a+')');
    }
    for( var k = 0; k < 4; k++ ) {
      console.log('rgb('+a+')');
    }
}

either way.
everything i’ve tried doesn’t do what i want. i’ve tried to create a function too, still kinda lost in it.

ok, so this is another version i created, almost does it again, but not quite right:

var n = [];
for( var i = 0; i < 25; i++ ) {
    n.push(i);
}

var a = [];
n.forEach( function() { 
    for(j = 0; j<3;j++) { 
        var b = j
    }
    return a.push(n); 
});
console.log(a);

outputs 25 arrays with arrays 0-25;

i know it’s something to do with arrays of arrays. thats the where i fail always, foreach loops and arrays have always been my downfall.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:42:32+00:00Added an answer on May 22, 2026 at 2:42 am

    You use three loops:

    // READ THE BELOW BEFORE YOU RUN THIS
    var r, g, b, rarray, garray, barray;
    rarray = [];
    for (r = 0; r < 256; ++r) {
        garray = [];
        rarray.push(garray);
        for (g = 0; g < 256; ++g) {
            barray = [];
            garray.push(barray);
            for (b = 0; b < 256; ++b) {
              barray.push({r: r, g: g, b: b});
            }
        }
    }
    

    That produces an array (rarray) of arrays (each garray), each of which in turn contains an array (each barray) of objects. Each object has an r, g, and b value.

    Note that as selbie points out, you’ll end up with a total of 256 * 256 * 256 = 16,777,216 entries spread across the various arrays. You’ll have 256 * 256 = 65,536 barrays and 256 garrays. In all, that’s 1 + 256 + (256 * 256) + (256 * 256 * 256) = 16,843,009 objects. Since all of these objects (including the arrays) are actually key=value maps, each entry will have a key portion and a value portion, and so just the references to those will be four bytes each. Then there’s the actual key data for the array entries (1-3 characters = 2-6 bytes minimum) and the key data for each of our 16M final objects (which have three keys each, r, b, and g). Plus some other overhead. The details will vary a lot by JavaScript environment, but we’re probably talking no less than (waves hands) 32-64 bytes per entry, for a total in memory of ~538MB-1076MB. Now, that’s not a lot by modern computer standards, but it’s a lot for a web page. 🙂 I used Chrome to do it, and it took ~1GB (did it in about 4 seconds — gotta love Chrome). In contrast, after several minutes and >2GB of RAM use, I gave up on Firefox. I wouldn’t go near this in IE with a barge-pole.

    Live example (using only 0..4 rather than 0..255).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this was asked somewhere in another question, but the wording used there
There's this already populated database which came from another dev. I'm not sure what
Im sure this is pretty simple but I just cant seem to find the
I'm sure this is a newbie question, but every time I've compiled/dl'ed a new
I'm sure this is probably something simple so I apologize ahead of time. This
I'm sure this is elementary, but I am stumped. The example is grossly over-simplified
I am sure this is really dumb, but I just cant seem to understand
I don't have much experience with javascript so I' not sure if/how this can
I'm sure glad this site is here. It has helped me out every time.
Pretty simple question this time around. I have an application that communicates with another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.