How to understand Incrementing variables in JS? [duplicate] - javascript

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

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

The Values given from Prompt and used in for loop is not working [duplicate]

This question already has answers here:
Sum of two numbers with prompt
(10 answers)
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Closed 9 months ago.
I am Beginner in Javascript, and created a program Adding Indeces of an Array numbers would come equal to the target given, and Printing the indeces as an output.
eg - Input is nums array= [1,2,3,4,5] and target = 9
adding 3th & 4th element would give the target value (5+4=9), so output would be 3,4
If i pass the values from Prompt, the code is not working. However if i pass the values directly, the code is working fine. I do not know why is this happening, is there any concept, that i am lacking. Please give your suggestions :)
Code is Below:
// taking array from user
let nums = [];
let size = 5; // Array size
for (let a = 0; a < size; a++) {
nums[a] = prompt('Enter array Element ' + (a + 1));
}
// taking Sum Target from user
let sum = prompt("Enter the Sum Target");
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === sum) {
alert(`The Two indices are ${i} and ${j}`);
}
}
}

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.

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