Javascript code goes into infinite loop - javascript

the following code goes into infinite loop and a crash in the webpage I need to know what's wrong with it?
for (var i = 0; i < 2; i+1) {
for (var j = i; j < 8; j + 2) {
console.log(arr[j].Qu);
}
console.log(arr[i]);
}

i+1 doesn't update i's value, therefor, the i always has value 1, as it takes 0+1 in every run, thus never being > 2 and never ending
You need to change it with i++, like this
for (var i = 0; i < 2; i++) {
Also, as #Xufox points out, udpate your J loop with
for (var j = i; j < 8; j += 2) {
i+1 is not an assign operation, that's why you need to assign the value urself. i++ and j+=2 translate to
i = i+1;
j= j+2;
and the result of the righthand operation is self-assigned to the variable

Value is not assigned back to variable.
for (var i = 0; i < 2; i+=1) { // i++
for (var j = i; j < 8; j+=2) {
console.log(arr[j].Qu);
}
console.log(arr[i]);
}

i+1 doesn't modify i value.
You could write instead i++.
Similarly, j + 2 doesn't update j.
You should write j += 2.
Here is the corrected code :
for (var i = 0; i < 2; i++) {
for (var j = i; j < 8; j += 2) {
console.log(arr[j].Qu);
}
console.log(arr[i]);
}

for (var i = 0; i < 2; i+=1) {
for (var j = i; j < 8; j+= 2) {
console.log(arr[j]);
}
console.log(arr[i]);
}

Related

Problems with code in JavaScript with loops

I learn loops in JavaScript with for-loop and I have this code (j) that does not work with me I don’t know why?
let start = 1;
let end = 6;
let breaker = 2;
for (let i = start; i <= end; i++) {
console.log(i);
for (let j = breaker; j <= end; i++) {
console.log(j);
}
}
You never increase j thus you get endless loop, try replacing
for(let j = breaker; j <= end; i++)
using
for(let j = breaker; j <= end; j+=1)
You changed i and j in inner loop
let start = 1;
let end = 6;
let breaker = 2;
for (let i = start; i <= end; i++)
{
console.log("i => ", i);
for(let j = breaker; j <= end; j++)
{
console.log("j => ", j);
}
}
`

maybe you just know what makes the asterisks on this loop so much

var s = '';
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 10; j++) {
s += '*'
}
s += '\n';
console.log(s)
}
maybe you just know what makes the asterisks on this loop so much?
even though I only want to repeat it for 5 lines, but why are there so many results?
[1]: https://i.stack.imgur.com/7zzOu.png
Your code is producing the following result:
Because not resetting the var s inside the outter iteration.
Instead, you may do:
for (let i = 0; i < 5; i++) {
let s = '';
for (let j = 0; j < 10; j++) {
s += '*';
}
s += '\n';
console.log(s);
}
which will produce the expected result.

JS - Convert nested forloops into single loop

I need to convert this nested loop into a single loop.
This is the loop with the scenario:
First incrementer is i which starts from 0 and should run till 10
Second incrementer is j which starts from where i left off + 1 and runs till 10
.
.
My Nested Loop
for (var i = 0; i < 10; i++) {
for (var j = i + 1; j < 10; j++) {
if (some_condition) {
do_sth()
}
}
}
My Attempt at conversion
var i = 0;
while (i < 10){
var j = i + 1;
if (j < 10) {
if (some_condition) {
do_sth()
}
j++;
}
i++;
}
Unfortunately, my attempt doesn't produce the expected results.
The second snippet does not give the output which the first snippet delivers.
Can anyone please suggest me what my mistake is or provide me a better solution to achieve my target?
Thanks!
Not sure it improves readability complexity, but the following should produce the same.
var i = 0, j = 1;
while (i < 9) {
console.log(i, j);
j += 1;
if (j >= 10) {i += 1; j = i + 1}
}
You need to update i inside else statement or use continue. And declare j outside of the while body.
But keep in mind that this neither change "the order of complexity" nor "optimise" your code.
var i = 0;
var j = 1;
while (i < 10) {
if (j < 10) {
if (true) {
console.log(i, j)
}
j++;
} else {
i++;
j = i + 1;
}
}
You could adjust the loop lenght of i and check if j is greater or equal than 9, then increment i and start with fresh j.
var i = 0,
j = 1;
while (i < 9) {
console.log(i, j);
// do you stuff
if (j >= 9) j = ++i;
j++;
}
.as-console-wrapper { max-height: 100% !important; top: 0; }

What's the difference between these For loops that makes one crash?

This is from Codecademy's Javascript lesson "Search Text For Your Name". The following works:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j])
}
}
}
However, when I replace i + myName.length with j + myName.length, it's crashing. In full:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < j + myName.length; j++) {
hits.push(text[j])
}
}
}
I'm not getting any errors when I run this, which led me to believe that it's just stuck in an infinite loop, except that when I place a console.log marker within the For loop in question, it doesn't print anything.
What's the reason for it crashing?
j < j + myName.length; j++
j never reaches the end. You're incrementing it, but you compare it against number that is always larger than itself (assuming myName.length is > 0). The conditions for the loop is always satisfied, causing it to run forever.
It crashes because it's an infinite loop.
Here is your second example, with the static variables converted to their values:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for (i = 0; i < 42; i++) {
if (text[i] === 'Z') {
for (var j = i; j < j + 5; j++) {
hits.push(text[j]);
}
}
}
Specifically, your inner for condition is causing the infinite loop:
for (var j = i; j < j + myName.length; j++) {
or with myName.length replaced by its value, 5:
for (var j = i; j < j + 5; j++) {
j will always be less than j + 5 so the loop continues without end, consuming memory until crash.

Javascript and for loops issue

Is something wrong with my code? I was expecting my code:
years=new Array();
for (i = 0; i < 5; ++i) {
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
to print something like:
[array[12],array[12],array[12],array[12]]
but the result that i get is:
[array[60],array[60],array[60],array[60]]
Create a new player array inside the first for loop. The problem with your code is that the values were being pushed into the same array instance.
var years = [];
for (i = 0; i < 5; ++i) {
var player = [];
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
As an addition to the correct answer already, please use var to declare your variables:
for (var i=0; i < 5; ++i) {
var player = [];
for (var j = 1; j < 13; ++j) {
...
Otherwise, it will use i as a global variable, which could end poorly if you have two functions looping at the same time, e.g.:
function loopone() {
//wouldn't expect this to be an infinite loop eh?
for (i=0; i < 100; i++) {
looptwo();
}
}
function looptwo() {
for (i=0; i < 10; i++) {
}
}

Categories