0
Why doesn't it do anything?
If you click the body, the pixels should change, but it actually does nothing. Why? https://code.sololearn.com/Week56QD099m/?ref=app
16 ответов
0
You accidently wrote i++ instead of i2++, so i2 is never increased.
Such a tiny error in some great code! I guess you need to get used to it if you want to be a programmer! 😁
+ 2
Yes, I already did. Now I'm going to stop the pixels from being "teleported" from the right to the left.
⬛⬛⬛ ⬛⬛⬛
⬛⬛⬜ ≠> ⬛⬛⬛
⬛⬛⬛ ⬜⬛⬛
+ 1
Is this what you want? I managed to make your code a lot shorter. If you want to make the existing white pixels turn back to grey each time, remove the // in front of turnOff(), otherwise you can delete the function.
PS: I think the comments sometimes make it hard to read sometimes. You could put all the comments at the end of each section.
https://code.sololearn.com/WaeNMsczF5Jh/?ref=app
+ 1
James,
It is good that you found the errors and correct them, but
I believe, the best way is to help is to just guide the person and let him
correct his code.
+ 1
C. Scheler,
One sugestion: Do not use comments in the middle of a statement.
like this line:
wp/*white pixels; haha, finally I commented what my variable names mean.*/ = [Math.ceil/*ceil = rounding up (for remembering;)*/(Math.random()*396)];
Instead, put comments above the line or after the statement. I personaly prefer above:
// wp = white pixels
// ceil = rounding up
wp = [Math.ceil(Math.random()*396)];
This makes reading your code easier.
+ 1
AARRRGGGG
+ 1
Now, just change your code at line 50 to something like this, so that the value in the wp array is updated.
var nextLocation = pnp[Math.floor(Math.random()*pnp.length)];
cp(nextLocation);
cp(wp[i]);
wp[i] = nextLocation;
And it works!! 🎉
+ 1
It sometimes gets stuck in the top corners now... not sure why, but if you change the 0 to a 1 on line 45, this stops.
I'll leave you to it now. Cool code! Good luck!
+ 1
I already fixed that.
0
What are you trying to do?
0
What do mean by "should change" exactly? Do you want the four white pixels to turn grey, then 4 other random grey pixels to turn white?
0
I want the bright pixels to "move".
0
James,
I don't want to add more bright pixels. Each bright pixel should move to a black pixel next to, above or underneath it.
Example:
First:
⬛⬛⬛
⬛⬜⬛
⬛⬛⬛
The red points are the possible points where the white pixel could go (var pnp):
⬛🔴⬛
🔴⬜🔴
⬛🔴⬛
One of the red points will be selected (randomly) and the pixel will move to this position:
⬛⬜⬛
⬛⬛⬛
⬛⬛⬛
or
⬛⬛⬛
⬛⬛⬜
⬛⬛⬛
or
⬛⬛⬛
⬛⬛⬛
⬛⬜⬛
or
⬛⬛⬛
⬜⬛⬛
⬛⬛⬛
0
Ah, sorry, I understand now.
I'm investigating, and I've found that your for loop on line 44 runs infinitely, because you have accidently written i++ instead of i2++. That is why your code keeps crashing, too.
0
It's impossible for the for loop on line 44 to run infinitely.
The loop is for(var i2=0; i2<pnp1.length; i++)
pnp1.length is 4.
So it would be
var i2 = 0;
//code
i2 = 1;
//code
i2 = 2;
//code
i2 = 3;
//code
i2 = 4;
//i2 is not < 4.
//end
0
The following code will avoid that the white pixels jump from one side to the other:
// possible_moves = the array of moves for the present pixel (pnp1 in your code)
// pixel = the current index in the for loop inside function 'live'
// num_cols = 18 in your code
possible_moves
.filter(i => !(pixel%num_cols == 0 && i == pixel-1))
.filter(i => !((pixel+1)%num_cols == 0 && i== pixel+1));