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 8 years ago.
Improve this question
I am creating a slider with my own custom steps with Javascript / jQuery.
Here I am using a for loop, but somehow my function only work, when i put a while loop inside it.
First code (Didn't work): - without a while
var steps = '';
// Setting up the steps according to the number of slides
for( var i = 0; i < $itemsCount; ++i ) {
var step = '';
// Find step number and step text
var step_text = $items.eq(i).attr('data-title');
var step_count = i + 1;
// current step will have the class 'current'
var step = i === current ? '<li class="step current"><span data-step="'+ step_count +'">'+ step_text +'</span></li>' : '<li class="step"><span data-step="'+ step_count +'">'+ step_text +'</span></li>';
i++;
steps += step;
}
var navSteps = $( '<ul class="steps"/>' ).append(steps).prependTo($slider);
Second code (Did work): - with a while
var steps = '';
// Setting up the steps according to the number of slides
for( var i = 0; i < $itemsCount; ++i ) {
var step = '';
// Find step number and step text
while (i < $itemsCount) {
var step_text = $items.eq(i).attr('data-title');
var step_count = i + 1;
// current step will have the class 'current'
var step = i === current ? '<li class="step current"><span data-step="'+ step_count +'">'+ step_text +'</span></li>' : '<li class="step"><span data-step="'+ step_count +'">'+ step_text +'</span></li>';
i++;
steps += step;
}
}
var navSteps = $( '<ul class="steps"/>' ).append(steps).prependTo($slider);
It isn't really a problem, but I still wonder why the first one didn't work.
Can anyone please tell me why I have to use a while instead the loop?
In the first one, you're incrementing "i" twice: once at the end of the loop body, and once in the loop header (the i++ in the parentheses).
When you add the while loop, you basically make the for loop (mostly) irrelevant, because when the while loop exits the value of "i" will cause the for loop to exit also.
Related
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 1 year ago.
Improve this question
I'm currently learning javascript loop and right now i'm trying to make an exemple using the "For" loop but as i'm trying it keep telling me "longString is not defined".
the result i'm trying to have is to print 6 A like this "AAAAAA"
here is the code:
var longString = A;
for (var longString = A; longstring < 6; longString = longString + A) {
console.log(longString);
}
i'm sure the solution is simple but as a totally beginner i can't figure out how to solve it.
You have a typo in your break condition:
var longString = "";
for (longString = "A"; longstring < 6; longString = longString + "A") {
// Note the lowercase 's' ^
}
console.log(longString);
Also, if you want to get the length of a string, you can use string.length:
longString.length < 6;
Does this what you want?
var str = ""
for (i = 0; i < 6; i++) {
str += "A"
}
console.log(str);
Try this, the below code prints "AAAAAA" in one line.
var longString = 'A'
var str = ''
for (var i = 0; i<6; i+=1){
str = longString + str
}
console.log(str)
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);
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 8 years ago.
Improve this question
I am trying to produce the following HTML dynamically:
<ul>
<li>Butter Extra</li>
<li>Butter Extra</li>
</ul>
I was trying it this way:
var toppingval = 'Butter Extra,Butter Extra';
if (toppingval.indexOf(",") > 0)
{
var array = toppingval.split(',');
var uitaghtml = '<ul>' +
for (var z = 0; z < array.length; z++)
{
'<li>' + array[z] + '</li>' +
}
'</ul>';
}
But when I tried this it throws an exception in console as
Uncaught SyntaxError: Unexpected token for
Could anybody please let me know how to solve this?
You're trying to concatenate a for loop to a string. That is not a thing. Make a new variable that you continuously append to in your for loop.
var toppingval = 'Butter Extra,Butter Extra';
if (toppingval.indexOf(",") > 0) {
var array = toppingval.split(',');
var uitaghtml = '<ul>';
for (var z = 0; z < array.length; z++) {
uitaghtml = uitaghtml + '<li>' + array[z] + '</li>';
}
uitaghtml = uitaghtml + '</ul>';
}
You are trying to do something you cannot. Syntactically, you cannot leave open a appending operator + - which you are trying to do, but adding in li's into your ul. Try this:
var uitaghtml = $('<ul></ul>');
for (var z = 0; z < array.length; z++)
{
uitaghtml.append($("<li>" + array[z] + "</li>"));
}
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 am adding subrows below a main row in an html table depending on which main row button is pushed. Here is some of the code:
if (document.getElementById("hdnSub0")) {
roz0 = document.getElementById("hdnSub0").value || 0;
}
...
if (document.getElementById("hdnSub10")) {
roz10 = document.getElementById("hdnSub10").value || 0;
}
nroz0 = parseInt(roz0, 10);
...
nroz10 = parseInt(roz10, 10);
if (intMainRow == 0) {
i = nroz0 + 2
};
...
if (intMainRow == 10) {
i = nroz0 + 2 + nroz1 + 1 + nroz2 + 1 + nroz3 + 1 + nroz4 + 1 + nroz5 + 1 + nroz6 + 1 + nroz7 + 1 + nroz8 + 1 + nroz9 + 1 + nroz10 + 1
};
row = table.insertRow(i);
I would like to write a loop especially for the if(intMainRow...) but I just haven't been able to it figure out.
So use a loop!
Consider getElementById("hdbSub" + i), where i is the loop variable incremented from 0..10. You can then use the loop (or an intermediate array) to go 0..x depending on the value of intMainRow.
For example:
// Because nroz0 + 2, but nrozOther + 1
// (You could also encode with an if/condition in the loop, but I
// feel like showing it this way.)
var incrs = [2,1,1,1,1,1,1,1,1,1,1];
// Running total (I use i/j/k only for loops.)
var total = 0;
// Will loop [0, intMainRow], an inclusive range, because of "<=".
// We loop once for each "field" and add the result in total.
for (var i = 0; i <= intMainRow; i++) {
var elm = document.getElementById("hdnSub" + i);
if (elm) {
// Add the value to the total along with
// the increment (+2/+1) for this index.
// (Each loop "adds" the next value to the equation
// written manually in the the original code.)
var v = parseInt(elm.value, 10) || 0;
total += v + incrs[i];
}
}
row = table.insertRow(total);
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();