JavaScript for-loop execution [closed] - javascript

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 8 years ago.
Improve this question
I wrote a program to have that particular output in javascript:
i=2 j=1
i=3 j=2
i=1 j=3
i=2 j=1
i=3 j=2
i=1 j=3
and so on...
But the following code never results in the output I need.
c=2;
for (i=c; i<=3; i++){
alert(i);
if(i==1){
j=3;
}else{
j=j-1;
}
alert(j);
if(i==3){
c=1;
continue;
}
}

That's seems to an easy problem : you just have to count 1 by 1 with 2 variables (i and j), the differences is than you don't start at 1 for both.
You need to increment j and i at the same time.
// this for not making infinite loop
var nb_loop=0;
var max_loop=10;
var j=0;
for (var i=2; i<=3 ; i++){
nb_loop++;
j++;
console.log("i="+i+", j="+j); // or alert if you want
if (j>=3)
j=0;
if (i>=3)
i=0;
if (nb_loop>max_loop)
break;
}
Note: a while loop may be better than the "for"

I don't know if u need to use this, i reproduce your output (see console)
var c=1,
j = 2,
i = 0,
x = 2; // this a limit for loop to prevent infinite loop
for (c; c<=3; c++){
if(c == 1){
i = 2;
}else if(c == 2){
i = 3;
}else{
i = 1;
}
//alert("i="+i+" j="+c); uncomment to see alert
console.log("i="+i+" j="+c);
//you can change this condition to stop the loop where you need
if(c==3 && x != 0){
c = 1;
x--;
}
}
see this example, maybe can help:
http://jsfiddle.net/pvMby/1/

Related

I am not getting proper output with nested for-loop in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}

Javascript Question: Sum of all odd numbers to 100 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to find out the sum off all odd numbers from 1 to 100 using a for loop. Here is my code so far:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1);
sum += i;
}
console.log(sum);
I thought maybe using a while loop maybe the solution, but whenever I try it, I always get an infinite loop error. So I tried switching back to a for loop, but the answer does not come out right. I believe I am looping through each number instead of every odd number. Is my "if" condition not correct? Any help is appreciated.
Thank You
You have a ; just after your if statement, so the next line is not going to get executed.
Just removing the ; should work:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1)
sum += i;
}
console.log(sum);
use step 2
var sum = 0;
for (var i = 1; i < 100; i+=2) {
sum += i;
}
console.log(sum);

Javascript nested array and setting the variable [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 5 years ago.
Improve this question
for (i = 0; i < length1; i++) {
for (j = 0; j < length2; j++) {
for (k = j; k < length2; k++) {}
}
}
I just wondered... in the last loop where I initialize k to be equal to j, would it cause problems for me later on?
I'm assuming every time j increments by 1, k is set to the new value of j and then increments up to length2
You are correct that for each iteration of the j-for-loop, the innermost loop will begin at J's current value and iterate up to length2-1. If this is what you want, shouldn't be a problem.

creating a javascript function to reverse a word [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 7 years ago.
Hi am trying to create a javascript function to reverse a word, but it seems the for loop is not begin executed and the holder variable is not being append in the for loop.
function jump(str){
var holder ="";
var len = str.length-1;
for(var i =len; i == 0; i--){
holder += str[i];
}
return holder;
}
console.log(jump("Just do it!"))
Your loop is incorrect:
for(var i =len; i == 0; i--){
^^^
The loop body only fires if that middle condition is "true". On its first iteration, i is something like 10, which means 10 == 0 is NOT true, aka false.
You probably want
for(var i =len; i >= 0; i--){
instead.
I think this should work for you
var text = 'Just do it!';
var rev = text.split("").reverse().join("").split(" ").reverse().join(" ");
var result = str.split("").reverse().join("");
The loop
for(var i =len; i == 0; i--){
holder += str[i];
}
will only run when i is equal to zero - which won't be the case, since you set it up as the length of your (presumably) populated string. Try:
for(var i =len; i >= 0; i--){
holder += str[i];
}

In JS how to print string in while loop multiple times? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
var loop = function(){
while(loop<3){
console.log("I'm looping!");
loop++;
}
};
loop();
I tried in this way... i need to print above message 3 times... how to do it ??
Your function definition is correct.
You could also self-invoke your function like this
var func = function() {
}(); // notice () right here, without another call like func();
What about loops, there are several. Take a look.
var times = 4;
for (var i = 0; i < times; i += 1) {
console.log('I\'m looping! #'+i);
}
// or
console.log("\n");
var o = 0;
while (o < times) {
o += 1;
console.log('I\'m looping! #'+o);
}
// or
console.log("\n");
var u = 0;
do
{
u += 1;
console.log('I\'m looping! #'+u);
}
while (u < times)
Change the variable name , is the same of your function
var loop = function(){
var l = 0
while(l<3){
console.log("I'm looping!");
l++;
}
};
loop();

Categories

Resources