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