Search code examples
javascriptarraysconditional-statementskhan-academy

Javascript: Incorporating random( ) start


Please help with this program. It's a challenge on Khan Academy to make it rain using arrays, loops, and conditions. I'm supposed to:

  1. Add more drops to the arrays.
  2. Make it so that the drops start back at the top once they've reached the bottom, using a conditional.
  3. Make an array of colors, so that every drop is a different color.
  4. Make other things rain, like snowflakes (using more shape commands) or avatars (using the image commands).
  5. Make it so that when the user clicks, a new drop is added to the array.
  6. Initialize the arrays using a for loop and random() function, at the beginning of the program.

I've done #1, #2, and #5, and I can do #3 & 4...but I can't for the life of me figure out how to do #6. I know it has to do with a random(0, 400) command, but I'm not sure how to make it work.

Here is the code:

// need random x value
// need random start times

// position of the rain at top
var xPositions = [1, 50, 100, 150, 200, 250, 300, 350, 399];
var yPositions = [0, 0, 0, 0, 0, 0, 0, 0, 0];

// attempt at defining a variable for random start
// don't know what to do with it after this
var raindrops = [random(0, 400)];

// rain falling from top to bottom at specific intervals
var draw = function() {
    background(113, 218, 237);

    for (var i = 0; i < xPositions.length; i++) {
        noStroke();
        fill(21, 143, 173);
        ellipse(xPositions[i], yPositions[i], 10, 10);
         if (yPositions[i] < 390) {
    yPositions[i] += 5; //makes rain fall down
    } else {
        yPositions[i] = 0;
    }        
}

for (var j = 0; j < xPositions.length; j++) {
            noStroke();
            fill(21, 143, 173);
            ellipse(xPositions[j] + 25, yPositions[j]-50, 10, 10);
        }

    for (var k = 0; k < xPositions.length; k++) {
            noStroke();
            fill(21, 143, 173);
            ellipse(xPositions[k], yPositions[k]-100, 10, 10);
        }
    };


// click to add more raindrops
var mouseClicked = function() {
    xPositions.push(mouseX);
    yPositions.push(mouseY);
};

I appreciate your input.


Solution

  • I figured it out:

    // click to add more rain
    
    // position of the rain at top
    var xPositions = [1, 50, 100, 150, 200, 250, 300, 350, 399];
    var yPositions = [0, 0, 0, 0, 0, 0, 0, 0, 0];
    var rainspeed = [1, 4, 2, 6, 3, 8, 5, 3, 4];
    
    // rain falling from top to bottom at defined intervals
    var draw = function() {
        background(113, 218, 237);
    
        for (var i = 0; i < xPositions.length; i++) {
            noStroke();
            fill(60, 163, 232);
            ellipse(xPositions[i], yPositions[i], 6, 12);
        if (yPositions[i] < 395) {
            yPositions[i] += rainspeed[i]; //makes rain fall down
        } else {
            yPositions[i] = 0; //takes rain back to top
        }
        }
    
    };
    
    
    // click to add more raindrops
    var mouseClicked = function() {
        xPositions.push(mouseX);
        yPositions.push(mouseY);
        rainspeed.push(random(2,8));
    };