This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
Closed 7 years ago.
;(function () {
var n = readline(), r = 0;
for (var i = 0; i < n; i++)
r += readline().split(' ').filter(function(v){ return v == 1;}).length > 1;
print(r);
}).call(this);
Why we add ; before the function and why we don't put {} for the for loop?.
The parenthesis in the for loop are omitted because it's only one line of code, in that case you do not need them. The ; is a fail-safe if you include this in other scripts.
Related
This question already has answers here:
For (;;) loop explanation
(7 answers)
How can I process each letter of text using Javascript?
(24 answers)
Closed 2 years ago.
I am a junior developer learning JS. Here is the piece of code I am practicing today.
I see the line " (var x=0; x < str1.length; x++) " almost every example I work on.
Can someone please explain what the mechanics are doing? Why do we always set a var equal to 0?
What role does str1.length play in this line?
function vowel_count(str1) {
var vowel_list = 'aeiouAEIOU';
var vcount = 0;
for (var x = 0; x < str1.length; x++){
if (vowel_list.indexOf(str1[x]) !== -1)
{
vcount += 1;
}
}
return vcount;
}
console.log(vowel_count("The quick black panther."));
This question already has answers here:
javascript i++ vs ++i [duplicate]
(8 answers)
Closed 3 years ago.
I wonder why in this code counter is not saving after it is incremented?
I mean now the behavior is the same as if I used if (counter+1 % 2). But ++ operator supposed to permanently increment the variable.
let counter=0;
for (let i = 0; i < 10; i++) {
console.log(counter,'every')
if (counter++ % 2) console.log(counter,'odd');
}
I believe you want to have ++ in front of counter
let counter=0;
for (let i = 0; i < 10; i++) {
console.log(counter,'every')
if (++counter % 2) console.log(counter,'odd');
}
If you use the ++ operator after the variable it will only increment after the line code is executed, if you put it behind the variable it will increment before the line code.
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 5 years ago.
I'm trying to understand this code (check whether a string can be re-arranged to a palindrome) :
function canRearrangeToPalindrome(str){
var letterCounts = {};
var letter;
var palindromeSum = 0;
for (var i = 0; i < str.length; i++) {
letter = str[i];
letterCounts[letter] = letterCounts[letter] || 0;
letterCounts[letter]++;
}
for (var letterCount in letterCounts) {
palindromeSum += letterCounts[letterCount] % 2;
}
return palindromeSum < 2;
}
Could you explain line letterCounts[letter] = letterCounts[letter] || 0;
It's outside of if statement, how can we use || ?
Thank you!
If the index at letter in letterCounts has a falsy value then set the value at that index to 0
Here is the MDN documentation for Falsy:
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
If letterCounts[letter] has a value it will assign that value otherwise it will assign 0.
This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I have code on JavaScript.
var a = [];
for (var i = 0; i < 5; i++) {
a[i] = function () {
alert(i);
};
}
a[2]();
If I invoke a[2]() I expect to see a message with 2 but instead of this I see 5.
To fix it I can rewrite it like this:
for (var i = 0; i < 5; i++) {
(function (v) {
a[i] = function () {
alert(v);
}
})(i)
}
But I cannot understand how does it work. So why I need to wrap my function code to closure?
This question already has answers here:
Javascript run a function inside a loop every x iterations
(5 answers)
Closed 7 years ago.
How can i do alert on each third loop iteration
So basically alert ("Some text") on each third iteration, how can i make it?
for(var i = 1; i < 20; i++) {
alert("Some text");
}
Use modulo operator (%):
for(var i = 1; i < 20; i++) {
if (i % 3 === 1) {
alert("Some text");
}
}