Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am studying JS from freecodecamp and the instructor was making an app which counts people, and he had written the code like this:
let countEl = document.getElementById("count-el")
console.log(countEl)
function increment() {
count = count + 1
countEl.innerText = count
}
increment()
can i write it like this
let count = 0
function increment() {
count = count + 1
document.getElementById("count-el").innerText = count
}
increment()
where i dont have to define countEl separately .
The second snippet is definitely better. The first one is a no-go for several reasons:
the countEl variable is global, which makes the function fragile and impossible to test
resource acquisition is separated from the actual usage and there's no apparent connection between both
should the HTML element go away for some reason, the reference will be kept around, thus creating a memory leak
On a more general note, try to design your code to avoid globals altogether and to only request resources when they are actually needed and free them as soon as possible.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am using this method to find the frequency of words, however i am a bit confused with the whole code.
Here is the code:
function check(){
var word = document.querySelector('textarea').value.split(" ");
frequency ={};
word.forEach(function(i){
console.log(i)
if(!frequency[i]){
frequency[i] = 0;
}
frequency[i]+=1
})
console.log(frequency)
}
<textarea></textarea>
<button onclick = 'check()'>check</button>
I just wonder what does the i stand for and what does the frequency[i].
Could someone explain to me a little bit because I think this code is not quite friendly for me, the beginner.
foreach iterates over array, and as paramter has function into which is passed as parameter actual element of array. So i in this function is actual element of 'word' array.
for object frequency is frequency[i] the i'th element. At start, this object is empty, so frequency[i] will be undefined, but in foreach loop you filling this object with some values so in next iterations there may by some values
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to increase or decrease a variable in a exponential or speeding up manner in a piece of code that happens every frame.
This code gets executed every frame:
if (animationLenght > 1) {
animationLenght --
// implement speeding up value
...
}
Doesn't have to be exponential exactly but like that it accelerates (or decelerates)
The animation variable decreases linearly by one on every frame.
I want to increase or decrease a value in an non linear fashion, but like speeding up or slowing down smothly.
Any ideas?
You can check the Math.exp() function: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/exp
let expNum;
for(let i=0; i < length; i++){
expNum = Math.exp(i)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using the Grasshopper app on my phone and I do not understand an example they gave me for if then statements.
They give you the solution because I answered incorrectly, but I do not understand why the solution given is correct.
var todayWeather = 'rainy';
var tommorrowWeather = 'cloudy';
if (todayWeather === 'rainy') {
print('Bring an umbrella');
}
if (todayWeather !== 'rainy') {
print('Maybe the sun will come out');
}
They say the correct answer is 'Bring an umbrella'. But why is this what this code will produce if it is run?
translating the code to english:
create a variable named todayWeather and set it to rainy
create a variable named tommorowWeather and set it to cloudy
if the variable todayWeather is rainy (true) then
print to the screen Bring an umbrella
(close if conditional)
if the variable todayWeather is not rainy (false, because it is set to rainy) then
print to the screen Maybe the sun will come out
(close code conditional)
Note the print statements only execute if the condition is met.
Also the variable values are never changed by this code after they are set.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm using jQuery and I'd like to know whether it is good practice to save $("#myVar") to a variable first like this
var jQvar = $("#myVar");
or directly using it like this:
var myVar = $("#myVar").val();
$("#myVar").removeProp("disabled");
..
and when is the best time to use the other over the other? (var and NonVar)
Each time you are accessing an element it takes (little but still) time. So saving it to a variable makes sense in terms of performance and efficiency if you access the element more then once in the current scope.
So writing it like this:
var myVar = $("#myVar");
var myVal = myVar.val();
myVar.removeProp("disabled");
will be faster.
In your specific case, this is the difference for me:
Try for your self
var jQvar = $("#myVar");
- jQvar.val();
- jQvar.onclick(function(){
// do something here
});
- jQvar.onSubmit(function(){
// do something here
});
... obviously this is more faster, you can reduce redundancy of variable declaration, much neater and less clutter you don't want to scroll and find every variable in file right?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If the title isn't clear enough, which is probably the case, here's what I mean :
Is it better to do this :
function example() {
if (condition) {
//The whole function code
}
}
or
function example() {
if (!condition) {
return;
}
//The whole function code
}
What is the cleanest way ?
Edit : I am not asking for your opinion. I'm wondering if there's any rule/convention/performance improvement.
I prefer the second way, "golden path" style.
Since I started to write all of my code like that I've found it to be a lot easier to read later. Perform checks in the order that makes sense, and return as soon as anything unexpected happens.
Wrapping code in a new block also adds indentation, which is kind of a waste of screen real-estate.