+ 2

Could you help me ?

Please what's wrong with the code, I need to fix it, I made a whole program using 365 lines of html and javascript and it works great, but I want to make it lesser, Help me to get it done. https://code.sololearn.com/WsGVpH4xOpwe/?ref=app

15th Jan 2022, 7:32 PM
Mohammed
9 Respuestas
+ 6
I was going through your code and I realized that you use a lot of lines of code to do repetitive tasks. One example of that is the way you create variables. But, i believe you can see a pattern. For example, you have there the variables "r1", "r2", "r3". What you could do instead is create an array called "r". That way you could go through those variables in a "for" loop. Here is an example you could use for all of the variables. var count = new Array(); for(let i = 0; i < 4; i++){ count[i]=document.getElementsByClassName("counter"+i)[0]; } And doing so, everything on your code can now be executed with for loops! Because you do the same thing to r1 and y1 for example that you do to r2 and y2. That way you will save yourself tones of lines of code. Btw, did you feel while writing the code you were being repetitive?? You also used document.getElementsByClassName and then used the first element of that array. well, instead you could use document.getElementsById. You should check it out!!
15th Jan 2022, 9:55 PM
Alexandre
Alexandre - avatar
+ 4
Well, congrats!! It looks a lot nicer and easier to read for sure! I'm going to take another look to see if you can improve it a little more but it really is awesome right now. One thing you should do tho is, instead of using for(var i = 0; i < 4; i++){} to go through all the elements of an array of 4, you should instead use for(var i = 0; i < array.length; i++){} That way, imagine you want to increase the number of elements on that array, you will not need to go through your code changing those values. Nice job and thanks for the message on the code :)
16th Jan 2022, 7:18 PM
Alexandre
Alexandre - avatar
+ 3
Well, now you can implement those changes on your code and I can help doing so if you need :)
15th Jan 2022, 9:56 PM
Alexandre
Alexandre - avatar
+ 2
Yeah sure, go ahead :)
16th Jan 2022, 7:28 PM
Alexandre
Alexandre - avatar
15th Jan 2022, 7:32 PM
Mohammed
+ 1
Mr. Alexander for(let i=0; i< ♾ ; i++) {console.log("Thank you")} Thanks for help, The code became less than 200 lines;
16th Jan 2022, 5:31 PM
Mohammed
16th Jan 2022, 5:35 PM
Mohammed
+ 1
Alexander Yes I think the code can be lesser.
16th Jan 2022, 5:44 PM
Mohammed
+ 1
array.length Yes please check if there are some improvements could be done. But then I would like to discuss some questions if you don't mind.
16th Jan 2022, 7:25 PM
Mohammed