Uncaught SyntaxError: Unexpected token for [closed] - javascript

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>"));
}

Related

"X" is not defined - using "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 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)

How to recover 2 results in the same function [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 5 years ago.
Improve this question
I have a javascript function:
var query1 = {{repair_semestre1}};
var query2 = {{repair_semestre2}};
var result1 = [];
var result2 = [];
for (var i = 0; i < query1.LRU.length; i++) {
result1.push(formatName(query1.LRU[i], query1.Client[i], query1.round[i]));
}
for (var i = 0; i < query2.LRU.length; i++) {
result2.push(formatName(query1.LRU[i], query1.Client[i], query1.round[i]));
}
return {
result1: result1,
result2: result2
};
function formatName(lru, turnover, round) {
return "[" + lru + "," + turnover + "," + round + "]";
}
It return the same values in result1 and result2.
How can I recovre the values of the result2.
Can you help me please.
thank you.
Just a typo, in your second for loop you are taking values from query1 instead of query2
for (var i = 0; i < query2.LRU.length; i++) {
result2.push(formatName(query1.LRU[i], query1.Client[i], query1.round[i]));//original
result2.push(formatName(query2.LRU[i], query2.Client[i], query2.round[i])); //change to this
}

Need to add one by one value to comma separated List from array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to add one bye one value to comma separated list
my code
var Plist, Llist;
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist = // Add comma separated value
} else if (id.startsWith("L")) {
Llist = // add comma repeated value
}
}
please suggest better solution...
var Plist = "", Llist = "";
for (var i = 0; i < results.length; i++) {
var id = results[i].id;
if (id.startsWith("P")) {
Plist += id + ",";
} else if (id.startsWith("L")) {
Llist += id + ",";
}
}
if (Plist.indexOf(',') !== -1) {
Plist = Plist.substring(0, Plist.length - 1);
}
if (Llist.indexOf(',') !== -1) {
Llist = Llist.substring(0, Llist.length - 1);
}

Why doesn't my 'for' loop function work? [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 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.

How to convert a string into a series of digits in Javascript? [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 8 years ago.
Improve this question
What would be a good way to turn a string into a series of digits in Javascript (I'm not talking about converting "0.5" into 0.5, but more "Hello" into 47392048)?
Any idea appreciated.
Thanks!
You can use the ASCII value of each letter:
"a letter".charCodeAt(0);
Ok, so given your comments, here is a (not widely tested) solution.
var str = "κόσμε 这是一条狗 é €";
$('#orig').after('<dd>' + str + '</dd>');
var result = "";
for (var i = 0, len = str.length, code, paddedCode; i < len; ++i) {
code = str[i].charCodeAt(0).toString();
paddedCode = code.length >= 8
? code
: new Array(8 - code.length + 1).join(0) + code; result += paddedCode;
result += paddedCode;
}
$('#nums').after('<dd>' + result + '</dd>');
var segments = result.match(/.{8}/g);
$.each(segments, function(k, v) {
$('#nums-segmented').after('<dd>' + v + '</dd>');
});
revertedString = '';
for (var i = 0, len = segments.length; i < len; i=i+2) {
revertedString += String.fromCharCode((segments[i] | 0));
}
$('#string').after('<dd>' + revertedString + '</dd>');
Run it at JSFiddle
The trick is to pad number and work with them as string when needed.

Categories

Resources