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)
}
}
Related
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 months ago.
I'm trying to run a for loop and print the current index without use let
here my code:
function init() {
for (var index = 0; index < 5; ++index) {
setTimeout(() => {
console.log(index);
}, index);
}
}
I expected to: 0 1 2 3 4
but i get 5 5 5 5 5
Once the Var replace in Let the problem will be solved
I want to stay with Var
How can the problem be solved?
Thanks
function init() {
for (var index = 0; index < 5; ++index) {
const i = index;
setTimeout(() => {
console.log(i);
}, i);
}
}
This should work.
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.
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 6 years ago.
Improve this question
so this is what I've achieved so far
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 1; i <= 10; i++)
arr[i] = 0;
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i < 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
so the scope for my case are
The sequence value is >= 1 and <=10
The sequence length is > 0
The sequence is an array of integer
Example 1
Input : sequence = [1, 10, 10]
Output : 10
Example 2
Input : sequence = [1, 3, 3, 3, 1]
Output : 3
It seems easy, I've tried to figure out but I cant find where is the mistake in my code, It's seems legit for me
As suggested by Maxim Krizhanovsky, the last loop should go from 1 to 10 instead of to sequence.length. Here is the working code
function arrayMode(sequence) {
var arr= [];
var mostFreq = 1;
for(var i = 0; i <= 10; i++)
arr.push(0);
for(var i = 0 ; i < sequence.length; i++)
arr[sequence[i]]++;
for(var i = 1; i <= 10; i++){
if( arr[i] > arr[mostFreq])
mostFreq = i;
}
return mostFreq;
}
The output is 10 and 3 respectively to your input.
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");
}
}