R
R
righto2019-07-09 08:43:23
JavaScript
righto, 2019-07-09 08:43:23

How do you like the quality of the code (javascript)?

I'm doing a test, I'm new to Javascript. Can you give some advice on the code?
https://codepen.io/anon/pen/LKXgQG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Antonov, 2019-07-09
@freislot

Correctly written above in the comment, for example, start by combining the addEventListener code. This can be done in many ways. Easily googled for addeventlistener on multiple elements.
This obj.innerHTML-- entry works (specifically a string is cast to a number), but it's not obvious to other people reading the code. This is the so-called "magic" of js, try to avoid lines like this, where non-obvious things happen, such as type casting. It is better to create a count variable, for example, and assign 15 to it and work with it, and display its value on the page.
Further, I would probably make the start() and stop() methods and describe the timeout logic in them, but I would not use the timeout either, I would take setInterval() and clearInterval() to stop.
Well, the rest is so trifles, I would, let's say, put all the selectors to the very top, there it seems to me that they have a place

let successLink = document.querySelector('.success-link'),
    successBlock = document.querySelector('.success-block'),
    failBlock = document.querySelector('.fail-block'),
    close = document.querySelector('.close'),
    closeLink = document.querySelector('.close-link');
    close2 = document.querySelector('.close2'),
    closeLink2 = document.querySelector('.close-link2');

And since you decided to use the ES6 syntax (let), it would be more correct to use const, because selectors do not change in code. Well, in general, formatting and indentation must be observed, not everywhere beautifully, let's say so :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question