So I solved the twoSum problem which you can find here: twoSum and I know there are better and more efficient solutions but for this one, I am just trying to understand why I get an empty array unless I reassign variable j inside the while loop. See code below. When I tried solving it by assigning j outside of the loop it doesn't work. See code comments.
My halfway answer/initial thought was that by assigning j's value outside of the loop its doing a direct assignment by value which means its value doesn't change since the interpreter only runs top to bottom and doesn't actually go back to check. So this would mean that if j were to be assigned by reference this problem would not occur?? I don't know if I'm on the right track
var twoSum = function (nums, target) {
let indecesArr = [];
let i = 0;
//j declaration without assignment
let j;
//let j = i + 1; //this will cause return to be empty array
while (i < nums.length) {\
//j's value has to be reassigned here otherwise return is an empty [] array
j = i + 1;
while (j < nums.length) {
if (nums[i] + nums[j] === target) {
indecesArr.push(i, j);
}
j++;
}
i++;
}
return indecesArr;
};
console.log(twoSum([3, 2, 4], 6)); //output [1,2]
You are incrementing the value of j in the inner while loop.
So when i =0 meaning nums[i] = 3 then j increments till the very end as their is no matching pair.
So next time when i = 1 & so on j has already traversed till the end of the array. Code inside the second loop never executes from i=1,hence no matching pair
I found the code for solving this problem, but I cannot figure out the logic of the solution.
let n = 10;
nextPrime:
for (let i = 2; i <= n; i++) {
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
alert( i );
}
I can't understand how the second for works, if it has an increment like the first, then the result should have been all numbers from 2 to 10, since the beginning of both for is the same number (2) ..
Can you please explain each iteration, for example why 4 is not displayed?
It’s using label to break the inner loop when it finds number is not prime . Outer loop is iterating the number and inner loop checks if it’s divisible between 2 to number.
Label break
the continue nextPrime used for just continue in the loop to the next iteration without finish all the code inside the loop.
like:
data:
for (let i = 1; i <= 3; i++) {
if(i === 2)
continue data;
console.log(i)
}
here when I get to I equal to 2 the code continue to the next number without continue to the rest of the code
I wrote a code to loop through an array and when I used while and the continue statement inside it I got stuck in infinite loop:
function loop(){
var x = [1,2,3,4,5,6,7,8]
var i = 0;
while ( i < x.length){
if (x[i] % 2 == 0){
continue;
}
i++
}
console.log(x[i])
}
loop()
I tried to add the i++ outside the loop and inside it I got the same result.
The expected result is 1,3,5,....etc the actual result is infinite loop.
The issue is that i should increment whether or not the condition is true or not, otherwise it'll continue to test the same value over and over again indefinitely.
function loop(){
const x = [1,2,3,4,5,6,7,8]
for(let i = 0; i < x.length; i++){
if (x[i] % 2 == 0){
continue;
}
console.log(x[i])
}
}
loop()
Modern JS:
function loop(){
const x = [1,2,3,4,5,6,7,8]
return x.filter(n=>n % 2 !== 0);
}
const res = loop();
console.log(res);
continue goes immediately to the next iteration of the loop, skipping the rest of the loop body. So it skips over the i++, and the next iteration tests the same x[i]. Since nothing has changed, the condition succeeds again, so it keeps doing the same thing.
You just want to skip over the console.log(x[i]) statement, not everything in the body.
Also, you didn't put the console.log(x[i]) inside the loop at all, so even if the loop worked correctly you wouldn't have printed out the odd elements; it would just print x[i] after the loop is done, and since i then would be outside the array, it would print `undefined.
function loop() {
var x = [1, 2, 3, 4, 5, 6, 7, 8]
var i = 0;
while (i < x.length) {
if (x[i] % 2 != 0) {
console.log(x[i]);
}
i++
}
}
loop()
If you really want to use while and continue, you can put i++ before the test. But then you have to subtract 1 from i when using it as the array index.
function loop() {
var x = [1, 2, 3, 4, 5, 6, 7, 8]
var i = 0;
while (i < x.length) {
i++;
if (x[i - 1] % 2 == 0) {
continue;
}
console.log(x[i - 1]);
}
}
loop()
You could also use a for loop instead of while, since the i++ will be in the for() header, which is executed every time.
function loop() {
var x = [1, 2, 3, 4, 5, 6, 7, 8]
for (var i = 0; i < x.length; i++) {
if (x[i] % 2 == 0) {
continue;
}
console.log(x[i])
}
}
loop()
If your goal is to print out the odd numbers in the loop, try just printing them:
function loop() {
var x = [1,2,3,4,5,6,7,8]
var i = 0;
while ( i < x.length){
if (x[i] % 2 == 1){
console.log(x[i]);
}
i++;
}
}
loop()
What’s happening right now is that your loop is not increasing i, so when the number is even you continue right past i++ and never get past that number, leading to an infinite loop. This way, whether the number is odd or even you increment i.
It happens because you do not increase i in the condition.
Use this:
function loop(){
var x = [1,2,3,4,5,6,7,8]
var i = 0;
while ( i < x.length){
if (x[i++] % 2 == 0){
continue;
}
console.log(i);
}
console.log(x[i])
}
loop()
If you want to iterate through an array, use for loop
The following code (looping from 10 to 1) doesn't run in javascript and I can't figure out why. Any advice?
for (var x = 10; x >= 1; x--){
console.log(x);
}
The test in a loop says when to keep looping, not when to stop. When your loop starts, x = 10, so x == 1 is false, and the loop stops immediately.
It should be:
for (var x = 10; x >= 1; x--)
you are setting x = 10, then looping while x == 1, which it isn't so you never loop. You should use "x > 0". The test in the loop is "loop as long as this condition is true".
you are using for loop . for for loop syntax is :
for (statement 1; statement 2; statement 3) {
code block to be executed
}
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed.
so according to yours code :
for (var x = 10; x == 1; x--){
console.log(x);
}
it will first assign x to 10 then it will check condition which is x==1 it will return false and loop will stop. so current version will be:
for (var x = 10; x >= 1; x--){
console.log(x);
}
That's not working because the syntax is not correct. It should be:
for (var x = 10; x >= 1; x--){
console.log(x);
}
The x==1 expression is the stopping condition of the loop and, when it's false, loop stops.
After experimenting with the use of "i++" and "++i" I could not find a difference between their results when used in a 'for' loop.
For example:
for (var i = 0; i < 10; ++i) {
console.log(i);
}
would yield:
0
1
2
3
4
5
6
7
8
9
Shouldn't it be printing out the numbers from 1 to 10, as the iterator is being incremented before console.log(i) executes?
The "increment step" is executed after the loop body is executed. Given
for (a;b;c) {
d
}
the execution order is
a // initialize
b // condition - first iteration
d // loop body
c // "increment"
b // condition - second iteration
d // loop body
c // "increment"
...
b // condition - last iteration - break
So in your case:
var i = 0;
i < 10;
console.log(i); // 0
++i;
i < 10;
console.log(i); // 1
++i;
// ...
i < 10;
The difference between i++ and ++i is only relevant if you do something with the return value, which you don't.
Because the last clause of the for loop only happens at the end of the loop, as its own statement, the behavior of your loop is not affected by this difference. However, imagine you did something like this:
for (var i = 0; i < 10;) {
console.log(++i);
}
for (var j = 0; j < 10;) {
console.log(j++);
}
Then you'd see a difference. The first example would produce numbers 1-10, whereas the second would produce numbers 0-9. That's because f(j++) is equivalent to j += 1; f(j);, whereas f(++i) is more like f(i); i += 1;.
May I advise that while your testing is fine on whatever platform you are using, the standard construct is i++
Always code the standard and isolate various platforms and make exceptions as needed !!!
i++ Simply means increment 'i' by one.
I can speculate ++i means to add 'i' to itself eg if 'i' was 2 then it would then increment to 2,4,8,16,32
But I have never seen ++i used in many places.