Why doesn't it do anything? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

26th Jan 2019, 2:21 PM
C. Scheler
C. Scheler - avatar
16 Answers
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! 😁
26th Jan 2019, 7:46 PM
James
James - avatar
+ 2
Yes, I already did. Now I'm going to stop the pixels from being "teleported" from the right to the left. ⬛⬛⬛ ⬛⬛⬛ ⬛⬛⬜ ≠> ⬛⬛⬛ ⬛⬛⬛ ⬜⬛⬛
26th Jan 2019, 7:55 PM
C. Scheler
C. Scheler - avatar
+ 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
26th Jan 2019, 6:28 PM
James
James - avatar
+ 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.
26th Jan 2019, 6:32 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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.
26th Jan 2019, 6:39 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
AARRRGGGG
26th Jan 2019, 7:48 PM
C. Scheler
C. Scheler - avatar
+ 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!! 🎉
26th Jan 2019, 7:50 PM
James
James - avatar
+ 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!
26th Jan 2019, 8:02 PM
James
James - avatar
+ 1
I already fixed that.
27th Jan 2019, 11:37 AM
C. Scheler
C. Scheler - avatar
0
What are you trying to do?
26th Jan 2019, 3:43 PM
Ulisses Cruz
Ulisses Cruz - avatar
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?
26th Jan 2019, 5:54 PM
James
James - avatar
0
I want the bright pixels to "move".
26th Jan 2019, 5:58 PM
C. Scheler
C. Scheler - avatar
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 ⬛⬛⬛ ⬜⬛⬛ ⬛⬛⬛
26th Jan 2019, 6:48 PM
C. Scheler
C. Scheler - avatar
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.
26th Jan 2019, 7:34 PM
James
James - avatar
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
26th Jan 2019, 7:40 PM
C. Scheler
C. Scheler - avatar
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));
26th Jan 2019, 8:39 PM
Ulisses Cruz
Ulisses Cruz - avatar