It's better to define counter variables where they're used, like here:
Code:
for (var i = 1; i < bla; i++)
Also use
let instead of
var. The reason is that
var is valid within the entire function it was defined in, while
let is only valid inside the code block it was defined in.
For instance:
Code:
function f() {
for (var i = 0; i < 10; i++) {
// "i" is valid here
}
// "i" is also valid here
for (let j = 0; j < 10; j++) {
// "j" is valid here
}
// "j" is no longer valid here
}
It's more efficient because the memory can be freed sooner. It's ok to use
var if you prefer, though, because
let is quite a bit harder to read than
var, in my opinion.
For clarity's sake, avoid defining multiple unrelated variables, especially if they're not the same type.
... is ok, because they're related, and they're the same type (strings). But...
... is bad, because they're not the same type; "name" is a string, "age" is an integer.
It's just really hard to read, especially when you have lots of them, like here:
Code:
var name, age, treeNode, oldIndex, newIndex, depth, isFound;
Next, for counter variables inside
for loops, using the name "i" is usually better than "Counter".
The reason is because "i" is faster to type than "Counter", and because most programmers are used to
for (i = 0; ...) and similar. In both cases, we don't know exactly what "i" or "Counter" do, but since we're used to seeing "i", why not use it?
One example where it would make sense to use a different name would be:
Code:
for (let studentId = 0; studentId < students.length; studentId++) {
let student = students[studentId];
// do something
}
Another example, and quite classic in games, would be "x" and "y". They usually represent coordinates in 2D space (3D with "z"), so when you see "x" or "y" you have a pretty good idea of what they're used for.
Code:
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
map[x][y] = 0;
}
}
Next, I would split your script into 2 functions, like this:
Code:
function findCommonEvent(eventName) {
for (let i = 0; i < $dataCommonEvents.length; i++) {
let event = $dataCommonEvents[i];
if (event.name === eventName)
return event;
}
return null; // Event not found
}
function doStuff(eventName) { // Not sure what your code does, so I named it "doStuff" as a placeholder
let event = findCommonEvent(eventName);
if (event) {
// do stuff
}
}
This way, your code is cleaner. It's also easier to find bugs. If
doStuff ever misbehaves, it's easy to tell if the bug lies in
findCommonEvent or not. You can also reuse
findCommonEvent for other, unrelated functions.
The less stuff a single function does, the easier it is to read. If you ever look at your code after a break of several weeks, you don't have to spend quite as long to understand your code, especially if your functions have meaningful names.
By the way, there's no need to write code for
findCommonEvent. Arrays already have a function for that:
Code:
let name = "bla";
let doesEventNameMatch(event) {
return event.name === name;
}
let event = $dataCommonEvents.find(doesEventNameMatch);
If you only need that function once, you can also make it anonymous, like this:
Code:
let name = "bla";
let event = $dataCommonEvents.find(function(event) { return event.name === name; });