How to do something on each third for loop iteration [duplicate] - javascript

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");
}
}

Related

starting from 10, print odd numbers using JavaScript [duplicate]

This question already has answers here:
How to Reverse a for loop? [closed]
(4 answers)
Closed 6 months ago.
How can the loop start writing from 10 to 0.
Currently, the output is 1,3,5,7,9 but, I need it as 9,7,5,3,1
for (var i = 0; i <= 10; i++) {
if (i % 2 != 0) {
console.log(i)
}
}
for (var i = 10; i >= 0; i--) {
if (i % 2 != 0) {
console.log(i)
}
}

How to understand Incrementing variables in JS? [duplicate]

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."));

Why increment not saves in conditional operator? [duplicate]

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.

Javascript loop showing same value 10 times [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
Why is this function only sending the number 10 ten times. I want it to send 1... 2 ... 3 ... 4 ... 5 and so on
but instead its showing
10....10.... 10... 10... I'm not sure why it would.
How do I make a loop that returns distinct values?
for (i = 0; i < locations.length; i++) {
setTimeout(function() { alert("test"+i.toString()) ; }, 100);
}
How do I make a loop that returns distinct values?
You can do this by using a closure (pass i back into an immediately invoked function expression (IIFE)). This will maintain the value of i:
for (var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function() {
console.log("test" + i);
}, 100);
})(i);
}
To increment the timeout by using the i works the same way. Making sure to wrap the entire timeout call with the IIFE:
for (var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function() {
console.log("test" + i);
}, i * 100);
})(i);
}

Explain javascript code syntax [duplicate]

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.

Categories