Print numbers every two blocks in a for loop - javascript

I have the following loop:
for (let i=0; i<7; i+=2) {
for (let j=i; j<i+2; j++) {
console.log(j);
}
console.log('\n');
}
If I execute it I get:
0
1
2
3
4
5
6
7
But it only works on even conditions (0-7 = 8), if I instead put i<8, I get the same:
0
1
2
3
4
5
6
7
Which is bad, it must has returned 8 at the end, but instead doesn't print it. I expect my result when the condition is not even like this:
0
1
2
3
4
5
6
7
8
How can I achieve it? Thanks for your help.

You could use a single loop and add for each odd value a line feed after printing the value.
for (let i = 0; i <= 8; i++) {
console.log(i);
if (i % 2) {
console.log('\n');
}
}

Although Nina's answer is better, for completeness, I post a corrected version of the code that you started with:
let n = 8;
for (let i=0; i<=n; i+=2) {
for (let j=i; j<i+2 && j <= n; j++) {
console.log(j);
}
console.log('\n');
}

Related

Construct 2/3: Continuous Loop Function?

Using Construct 3 to build a game.
I want to change this function in the code below (which just randomly assigns a frame count in an array) to something that isn't random, but instead loops in order from indexes 0 to 10 and then back to 0 and then to 10 in order again and loop like that continuously.
random(1,Self.AnimationFrameCount)
Is there a random() equivalent for non-random?
// generator function - yields multiple times, maybe forever
function* oneToTenAndBack(N: number): Generator<number> {
while(true) {
for (let i = 0; i < N; i++) yield i;
for (let j = N; j > 0; j--) yield j;
}
}
let k = 0;
for (let num of oneToTenAndBack(4)) {
console.log(num) // 0 1 2 3 4 3 2 1 0 1 2
if (++k>10) break;
}
let gen = oneToTenAndBack(3);
for (let k = 0; k < 10; k++)
console.log(gen.next()) // {value: number, done: false}
Playground Link

while loop, JavaScript printing unexpected value

I have gone through this Question and Answer While loop inside while loop JavaScript
function getMaxLessThanK(n, k) {
let i = 1;
while (i < n) {
let j = i + 1;
while (j < n + 1) {
console.log(i, j)
j++
}
i++
}
}
when n = 5, what am I getting is
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
undefined
How to avoid this the last line undefined. what is the reason for it?
Can anyone help on this please?
Edit:
This is what the actual thing I am doing below.
If you're running it in a console and get an undefined like this:
This undefined indicates that your statement runs without returning a value. It's not a result of your console.log.
If you make the function return something:
function getMaxLessThanK(n, k) {
let i = 1;
while (i < n) {
let j = i + 1;
while (j < n + 1) {
console.log(i, j)
j++
}
i++
}
return 'return value';
}
Then you'll get

Problems in understanding javascript nested for loops [closed]

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 1 year ago.
Improve this question
I try to understand nested for loops in javascript but it's very confusing.
I have this code and I can't understand how it works:
let n = 5;
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++) {
console.log(j);
}}
In console I have : 0
1
0
1
2
0
1
2
3
And I'm trying to figure out which loop represent each number.
Run this code:
let n = 5;
let str = '';
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++)
str += j;
console.log("Outer: "+i+" Inner: "+str);
str = '';
}
Output is:
Outer: 0 Inner:
Outer: 1 Inner: 0
Outer: 2 Inner: 01
Outer: 3 Inner: 012
Outer: 4 Inner: 0123
As you can see, in the output above, inner loop (the one with variable j) doesn't run, because if you replace the variables with numbers it would be
0 < 0 (i < 0), which isn't true.
The best way for you to understand how nested loops work is to write each step and variable values on the paper, like so:
n = 5
STEP 1:
i = 0
i < n (0 < 5) TRUE
j = 0
j < i (0 < 0) FALSE inner loop doesn't execute
OUTPUT: "Outer: 0 Inner:"
str = ''
STEP 2:
i = 1
i < n (1 < 5) TRUE
j = 0
j < i (0 < 1) TRUE
str = 0
j = 1 (j++)
j < i (1 < 1) FALSE
OUTPUT: "Outer: 1 Inner: 0"
str = ''
And so on... Keep repeating this until the argument in the outer loop is false (i < n).
You must remember, in for loop the sequence of orders executed:
for(let i = 0; i < 5; i++) {
console.log('It works');
}
let i = 0; (this executes only once)
i < 5 (if true run 3 and 4)
run the code in loop
i ++
i < 5 (if true run 6 and 7)
run the code in loop
i++
etc.

Print sequence of numbers in javascript

I'm sure this is quite a simple programming question however, I cant seem to understand it...
I'm trying to make the console.log print out numbers like this - 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 - one for each line. I thought modulo could be used to make this happen, however, I cant seem to figure out how to use it.
Here is the code:
iteration = 16;
for (var i = 0; i < iteration; i++) {
if(i == iteration%4 )
console.log(i);
}
Yes, you need a single loop.
No, you do not need the remainder operator %. This would give you
0 1 2 3 0 1 2 3 ...
But instead you could divide the actual value by 4 and take the integer value for console.log.
const iteration = 16;
for (let i = 0; i < iteration; i++) {
console.log(Math.floor(i / 4) + 1); // offset for starting with 1
}
I suggest that you use two nested for loops, one for the rows and another one for the columns.
Here's an example of how i would do it:
const columns = 4;
const rows = 4;
//if you want to just console.log each number on a different line
for (let i = 1; i <= rows; i++) {
for (let j = 1; j <= columns; j++) {
console.log(i);
}
console.log("\n");
}
//if you want to add each number to an array, and then log the array
for (let i = 1; i <= rows; i++) {
let columnsArray = [];
columnsArray.length = columns;
columnsArray.fill(i);
console.log(columnsArray);
}
//if you want to just log the numbers, you can spread the array
for (let i = 1; i <= rows; i++) {
let columnsArray = [];
columnsArray.length = columns;
columnsArray.fill(i);
console.log(...columnsArray);
}
//or you could push the arrays in another one, and get a matrix!
const matrix = [];
for (let i = 1; i <= rows; i++) {
let columnsArray = [];
columnsArray.length = columns;
columnsArray.fill(i);
matrix.push(columnsArray);
}
console.log(matrix);
It was not clear the output that you wanted, so i got a little sidetracked and made an example for the different cases that came to my mind.

simple modulo not returning list of values I expected

I thought the following would list all numbers from 0 to 21 that are divisible by 7, i.e. 7, 14, and 21. Instead it returns 22. Why?
for (var i = 0; i <= 21; i++) {
if (i % 7 === 0);
}
console.log(i);
BTW, I have no programming background and I'm struggling to get my first language under my belt. The teeny tiny details are doing me in. Anyway, thanks for helping me see how placement of console.log affected the result.
because you don't print anything in your loop, only the final value of i.
Your current code, reformatted:
for (var i = 0; i <= 21; i++)
{
if (i % 7 === 0)
; // do absolutely nothing
}
// now that i == 22...
console.log(i);
what you mean to say:
for (var i = 0; i <= 21; i++)
{
if (i % 7 === 0)
console.log(i);
}

Categories