Increment a variable by 3 in a loop that increments by 1 - javascript

I want to create a loop where the "i" variable is incrementing by one (i++), and I want to add another variable "j" in the loop that increment not by one but 3 per 3 (so j+=3, and then the output looks like 0, 3, 6, 9, 12...).
I have tried so many thing, but here is my code that looks logic :
let j;
for (let i = 0; i < 24; i++) {
j = i += 3;
console.log(j); //It increments by 4, WTF ??
console.log(i); //Exactly the same whereas i should increments per 1
}
I also tried to create a variable "k" that is equal to "i" to leave "i" alone, but still doesn't work.
Thank you so much for your help guys :)
PS : Once solved, do you know how to make the variable j starts by 0 please ?

let j=0;
for (let i = 0; i < 24; i++) {
j+=3
console.log(j); //starts at 3, because in the first line of the function we say j = 0 + 3, so j=3, then once it loops again it gets +3 again, so it's 6 and so on.
console.log(i); //just increments by 1 each loop
Try this, is this what you were trying to achieve?

Like this - you can use comma separators in the initiator and loop statements in the for statement
for (let i = 0, j = 1; i < 24; i++, j += 3) {
console.log("i",i,"j",j);
}

Related

How to find square of n with a for loop javascript

Can someone walk me through the following code:
function square(n) {
let result = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
result += 1;
}
}
return result;
}
//
example:
console.log(square(5)) // 25
How does this for loop work in creating a square of an n number? I don't know where to start in approaching how this works.
The square of a number can be visualized as the number of squares in a grid. 5x5, for example, is 5 wide by 5 tall:
You can think of the nested loop in your code
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
result += 1;
}
}
emulating movement along the grid.
When i is 1, it's going along the first row. When j is 1, that corresponds to the first column. That corresponds to the first square. Next iteration corresponds to the first row, second column - etc, until i <= n is no longer fulfilled, at the end of the row. The next row then iterates n times, and so on.
You'll see that the number of total iterations (where result += 1 runs) is equivalent to n * n, as the nested loop implements.
In short, the inner(j) loop runs 5 times for every iteration of the outer(i) loop..
Basically,
for each i = 1, j runs 5 times
for i = 2, j runs 5 times //total 10 times
for i = 3, j runs 5 times //total 15 times
for i = 4, j runs 5 times //total 20 times
for i = 5, j runs 5 times //total 25 times
So the result gets incremented 25 times, and hence the answer is 25

How to iterate over a JSON object in chunks of 3?

So i have a json object that is being served by nodejs.
I'm wanting to make articles in rows of 3, then div's in rows of 3 below the articles (that contain the information for the articles.
for (var infoset in jsonObj){
createArtRow(jsonObj[infoset][info]);
createDivRow(jsonObj[infoset][info]);
// creates an article, then a div one at a time
}
I'm having issues, because i'm unsure how to join the for loop iterating over the object (only 3 at a time).
for (var infoset in jsonObj){
for (var i = 0; i < 3; i ++) {
createArtRow(jsonObj[infoset][info]);
}
for (var i = 0; i < 3; i ++){
createDivRow(jsonObj[infoset][info]);
}
}
// ideally creates 3 articles, then 3 divs at a time.
I hope that makes sense.
Use a loop that increments by 3 instead of 1.
for (var i = 0; i < jsonObj.length; i += 3) {
// here you can use jsonObj[i], jsonObj[i+1], and jsonObj[i+2] to create a row
}
You could use modulus funcion, which i think is a cleaner more readable approach:
let i =0
for (var infoset in jsonObj){
If (i % 3 == 0 ){
createArtRow(jsonObj[infoset][info]);
}
createDivRow(jsonObj[infoset][info]);
i++;
}
Modulus (%) function works by deviding 'i' by the number after the % sign.
If the remainder is 0 (so exactly dividable by 3), it will be true and execute the code.

Simple explanation of checking prime numbers

I would like someone to explain me, how this code work? Because I cannot understand exactly.
This code write in console all prime numbers between 2 and n.
let n = 10;
nextPrime:
for (let i = 2; i <= n; i++) {
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
console.log(i);
}
Prime number is a number that can be divided by 1 and itself and has no other dividers.
This code loops over <2...n) range to check if the number can be divided by it (i % j == 0). If it can be divided by that number, it means that i is not a prime number, so we continue nextPrime, which means ending current iteration and proceeding to the next one. If we never run continue for particular j, it means it's a prime number.
Since you asked about nested loops, I'll try to explain why they are like that here. For simplicity, let's assume we need all prime numbers up to 5 instead of 10. Your loop is as follows:
for (let i = 2; i <= n; i++) {
for (let j = 2; j < i; j++) {
That means that we're gonna check the following pairs of i and j, in order:
i = 3, j = 2
i = 4, j = 2
i = 4, j = 3
i = 5, j = 2
i = 5, j = 3
i = 5, j = 4
Here the function of inner for loop is to check whether the current value of i is prime or not.
Now how inner loop will do this ? See the basic definition if a number is divisible by only 1 and itself then it is prime no. But here the value of "j" is starting from 2 and going upto less than that number. So if "i" is divisible from any other no. then condition (i%j==0) will become true and code written inside that block will be executed.
And the code written inside it is continue nextPrime which will redirect the execution of program again to where nextPrime keyword is written. So the console.log(i) statement will not execute for that value of i.
And if condition not becomes true (means when it is prime) then it execute the last line which will print that no.

i++ loops without any problem, i+2 loops infinitely and crashes

I want to create a simple loop function that adds 2 every time it loops. However, even though I tell my for loop to stop if the number reaches or is less than 100, it goes past 100 and loops infinitely.
i++ works just fine:
function addTwo() {
for (i = 0; i <= 100; i++) {
console.log(i);
}
}
addTwo();
When I change it to i+2 it crashes:
function addTwo() {
for (i = 0; i <= 100; i + 2) {
console.log(i);
}
}
addTwo();
I expect the console to log:
0
2
4
6
8
...
100.
But instead it loops infinitely and crashes.
i+2 in your case does nothing. JS evaluates it and then does nothing with the calculated value, this means that i is never increased.
++ is a special operator that increments the variable preceding it by 1.
To make the loop work you have to assign the value of the calculation i+2 to the variable i.
for (i=0; i<=100; i = i+2) {
console.log(i);
}
or
for (i=0; i<=100; i += 2) {
console.log(i);
}
i++ increments i. But, i+2 doesn't update the value of i. You should change it to i += 2
function addTwo() {
for (i = 0; i <= 100; i += 2) {
console.log(i);
}
}
addTwo();
The third parameter of a for is the final-expression:
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.
In your case you are not assigning any value to i. You should replace it with something like this:
function addTwo() {
for (i=0; i<=100; i+=2) {
console.log(i);
}
}
addTwo();
i++ is a short hand for i += 1 which is called Increment Operator But i+2 or even i+1 will not increase the value of i. You need to increase it by assigning a new value to i. i = i + 2 or i += 2.
Number is one of the primitive types in javascript which mean you can't change it unless you use assignment operator =
Note: You are not using let or var with i this will make i a global variable.
function addTwo() {
for (let i = 0; i <= 100; i+=2) {
console.log(i);
}
}
addTwo();
for (i = 0; i <= 20; i++) {
console.log(i);
i++;
}
You can increase i twice Or
for (i=0; i<=100; i+=2) {
console.log(i);
}
you can use i+=2 it will increase value of i 2 times and set new value of i.

Nested Array in JavaScript

I have an array soter and a counter array. I want to get the the number of name which the count array will provide me. Is it correct ? I am a bit confused about the output. Can someone enligten me on this nested array loop in JavaScript ?
var soter = ['bp','mf','cc'],
count = [0,0,0];
for(var y = 0 ; y < soter.length; y++) {
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[y]) {
count[y]++;
That code seems correct to me, supposing the well formed object data and child SO_Ter .
So you go through the outer loop, positions 0 to 2, and for each one of them you will check that each of the items in data.SO_Ter is equal to the soter value.
If you find that value, you increment the count in 1.
Does it make sense?
To make it easier, it would be like:
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[0]) {
count[0]++;
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[1]) {
count[1]++;
for(var i = 0 ;i < data.SO_Ter.length; i++) {
if(data.SO_Ter[i].name == soter[2]) {
count[2]++;
So since you do it 3 times, you just replace those with an outer for loop.
UPDATE
count[0] represents how many times the word 'bp' has been found
count[1] represents how many times the word 'mf' has been found
count[2] represents how many times the word 'cc' has been found

Categories

Resources