Why increment not saves in conditional operator? [duplicate] - javascript

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.

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

Forloop count isn't defined in JS?

Tried doing this for a class assignment but for whatever reason it is saying the count is not defined. Any suggestions?
var num = [1,2,3,4,5,6,7,8,9,10]
for(var num = 0; count < 11; num++) {
if(num % 3 ===0);
console.log(num);
}
I think you mean to use the num variable instead of count.
for(var num = 0; num < 11; num++) {
You are defining the 'num' variable. Setting it to 0 and then running the 'for' loop, adding 1 to 'num' for each loop until 'num' is no longer < 11.
try this...
var num = [1,2,3,4,5,6,7,8,9,10];
for (var count = 0; count < num.length; count++) {
if (num[count] % 3 == 0)
alert(num[count]);
}
Typically, Javascript for loops will have this format:
for (i = 0; i < 11; i++) {
//i = 0 > starting index
//i < 11 > ending index
//i++ > index increment
}
The reason why you ran into your error is because count is never defined as a variable, whereas the variable 'i' in my example was defined when I set the value of i=0.
Instead of thinking that you are looping through the integers within the num array, think of it like you're looping through the indexes of num. So within every loop, the i variable will represent which index of the array you're currently focusing on.
Helpful tips:
make sure you utilize num.length to get the ending index of your for loop
Use indexes to reference integers in an array: num[0] == 1,
num[1] == 2, num[2] == 3 ...

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

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

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