Could you help me ? | Sololearn: Learn to code for FREE!

+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

1/15/2022 7:32:08 PM

Mohammed

9 Answers

New Answer

+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!!

+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 :)

+3

Well, now you can implement those changes on your code and I can help doing so if you need :)

+2

Yeah sure, go ahead :)

+1

https://code.sololearn.com/WPB0i3pR5DbH/?ref=app

+1

Mr. Alexander for(let i=0; i< ♾ ; i++) {console.log("Thank you")} Thanks for help, The code became less than 200 lines;

+1

https://code.sololearn.com/WWEXTKkNF2zU/?ref=app

+1

Alexander Yes I think the code can be lesser.

+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.