Troubleshooting a JavaScript project involving arrays: Let it pour!

I'm a beginner here and currently enrolled in the Khan Academy programming course, focusing on Javascript. I've hit a roadblock with a project called "Make it rain," where the task is to create raindrops falling on a canvas and resetting back at the top. Using arrays, I've successfully generated drops at random positions and made them fall through the canvas. However, I am facing difficulty restarting them at the top due to the array usage. Here's my code:

var dropX = [40];
var dropY = [0];
var snowX = [40];
var speed = 1.5;
var ex = 0 ;
var i = 0;
var fall = dropY[i];
noStroke(); 

var rain = function (){ 
 var fall = dropY[i];    
 var ok = dropX.length;     
for(var i = 0; i<100; i++) {
    if (fall<2100){
    background(192, 224, 237);
    fill(23, 123, 143);
    ellipse(dropX[i],dropY[i],5,5);
    fill(224, 213, 213);
    rect(snowX[i],dropY[i],5,5);
    dropY[i]+=speed;
    dropX.push(random(0,400)); 
    dropY.push(random(-1650,0)); 
    snowX.push(random(0,400));
}
}

for (i = 0;i<100; i++)    {
    i++;
    fill(45, 136, 189);
    ellipse(dropX[i],dropY[i],5,5);
    fill(255, 255, 255);
    rect(snowX[i],dropY[i],5,5);
}};

draw = function() {
rain();
};
Check out my project here!

The Khan Academy code editor uses processing.js from what I understand. Thank you for any assistance or advice!

Answer №1

Check out these statements:

let dropX = [40];
let dropY = [0];
let snowX = [40];

All of them are creating arrays with a single element. dropX[0] and snowX[0] are both set to 40, while dropY[0] is set to 0.

Also, make sure your loops iterate from 0 to 100, not just up to 40;

To correctly initialize your arrays, use the following code snippet:

let dropX = [];
let dropY = [];
let snowX = [];
let i;

for(i = 0; i < 100; i++) {
  dropX[i] = 40;
  dropY[i] = 0;
  snowX[i] = 40;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing the click function to malfunction after selecting a second item?

I'm struggling to understand why my click function isn't functioning properly. You can view the JSFiddle here: http://jsfiddle.net/adbakke/ve9oewvh/ This is a condensed version of my HTML structure: <div class="projectDisplay"></div&g ...

Differences between encoding URL variables in HREF and using JS window.location for onclick events

For some reason, this particular hyperlink is not functioning properly. I have a Javascript redirect (window.opener.location) where I pass several variables through the URL. The problem arises when these variables contain apostrophes. In PHP, I am using UR ...

Utilize a for loop in Vue.js to save a fresh array of objects in a structured format

Trying to achieve the following using Vue: I have two JSON objects that provide information on languages and the number of inputs per language. Using nested loops, I display all the inputs. My goal is to create an object for each input with additional det ...

Loading images in advance using JavaScript

I have been searching through multiple forums and cannot seem to figure out the issue. The problem I am encountering is related to a website where there are three images with a hover effect. When the page first loads, there is a delay in loading the backgr ...

Discovering Ways to Navigate and Search Within XML and JSON Data

Currently, I am receiving a balance sheet report in the form of a JSON or XML object. The JSON format resembles the Row object containing several nested arrays. Among these are different RowTypes such as Header, Section, and Summary Row. My goal is to iden ...

Receiving a JavaScript function's output with Python Selenium

Currently, I am in the process of scraping data from a specific webpage. The focus is on extracting the return string value from a Javascript function snippet. The target value to be extracted is indicated as "2227885" Attempting to achieve this ...

Is it necessary to reset (local storage, session storage) once the countdown timer reaches zero?

I have successfully implemented a countdown timer using session storage in my code. However, I am looking to enhance its functionality further by: Clearing the local session if the user leaves the site before the countdown timer finishes. Automatically c ...

Unable to access the .env file in Vue.js when executing cross-env NODE_ENV=development webpack-dev-server --open --hot

I'm attempting to utilize an .env file for storing local variables, but I am encountering an issue where they appear as undefined when I try to log them. Here is a snippet from my .env file (located at the root of my project): VUE_APP_STRAPI_HOST=htt ...

What are the steps to implement infinite scrolling in a Vue.js application?

Currently, I am attempting to implement an infinite horizontal scroll section in vuejs for a selection of products. However, I am facing difficulties achieving the infinite effect. My approach so far involved removing the card that goes out of view and add ...

What is the best way to use jQuery to emphasize specific choices within an HTML select element?

Seeking help with jQuery and RegEx in JavaScript for selecting specific options in an HTML select list. var ddl = $($get('<%= someddl.ClientID %>')); Is there a way to utilize the .each() function for this task? For Instance: <select i ...

Special characters like greater/less than signs have not been properly encoded

I am currently facing an issue in regards to escaping strings on the server side (using Node.js & Express.js) that contain greater/less than signs (<, >). Below is the code snippet I'm using on the server side: socket.on('message', function ...

Creating a cutting-edge mobile application using PhoneGap and Node.js

I have a vision to develop an app similar to a mobile messenger, but I am not a seasoned programmer. My knowledge of JavaScript is at an intermediate level, although I haven't utilized it for any significant projects. The main focus of the app would i ...

Transforming the *.vue file into a *.js file for deployment on a content delivery network

Is there a way to compile Component.vue files into JavaScript files for use with the Vue CDN in HTML? For example, consider the following component Component.vue: <template> <div class="demo">{{ msg }}</div> </template& ...

The jquery script tag threw an unexpected ILLEGAL token

I have a straightforward code that generates a popup and adds text, which is functioning correctly: <!DOCTYPE html><html><body><script src='./js/jquery.min.js'></script><script>var blade = window.open("", "BLA ...

Generating a dynamic method for uploading multiple files

Is there a way to dynamically generate multiple upload forms upon clicking a button? I currently have code that allows for uploading one file, but I would like to be able to add additional forms for each file. In other words, if I need to upload 7 files, I ...

Tips on showing content while filtering is taking longer with AngularJS

When working in Angular, I encountered a situation where filtering a large amount of data in a table was slowing down the process. To address this issue, I wanted to display a spinner every time a filter operation was in progress. Here is an example simil ...

Validation using Joi allows for a field to be optional, but if it is included, it must be a positive integer

I am facing a challenge with a field in my JOI schema that I want to make optional, allowing for both undefined and null values. However, if a value is provided, it must be a positive integer. How can I accomplish this? So far, I have attempted the follow ...

The website flickers in and out as it loads

My site keeps flashing whenever it loads. Despite trying the solutions recommended in this stackoverflow post, I have had no success. Every page on my website loads a nav.html file using this code: $.get("nav.html", function(data){     $("#nav-placeho ...

Update the image with a new one using jQuery and PHP, then refresh the page to see

I'm currently utilizing simpleImage, a PHP image manipulation library. My aim is to externally rotate an image using AJAX and replaceWith functions. It successfully replaces the image, but unfortunately it doesn't refresh after rotation. Here&ap ...

Encasing the app component with a context and encountering the issue: TypeError - (destructured parameter) does not have a defined value

My goal is to wrap all components under the app in a context to provide specific functionalities (as evidenced by my UserContext component). import React, { useState, createContext, useContext } from 'react' const Context = createContext(); exp ...