"X" is not defined - using "For loop" in Javascript [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 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)

Related

Get palindrome length from string [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 4 years ago.
Improve this question
I have one string as "testaabbaccc" in this string we contain palindrome as "abba" and it's length is 4 but how can we identify this with a JavaScript code.
var string ="testaabbaccc"
Need Output as abba is palindrome and length is 4
You can use this article and modify it to your needs.
Working demo
function isPalindrome(s) {
var rev = s.split("").reverse().join("");
return s == rev;
}
function longestPalind(s) {
var maxp_length = 0,
maxp = '';
for (var i = 0; i < s.length; i++) {
var subs = s.substr(i, s.length);
for (var j = subs.length; j >= 0; j--) {
var sub_subs = subs.substr(0, j);
if (sub_subs.length <= 1)
continue;
if (isPalindrome(sub_subs)) {
if (sub_subs.length > maxp_length) {
maxp_length = sub_subs.length;
maxp = sub_subs;
}
}
}
}
return maxp;
}
console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

i want to create an empty array to fill it later [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
hi guys i tryed to create an empty array to fill it later. I post my code so you can help me. XD
"use strict"
var array = new array(6);
for(var i = 0; i <= 5; i++){
do {
var number = prompt("Put the element" + (i+1), 0);
}while(isNaN(number));
arr.push(number);
};
write.data(array);
It appears you may have copy and pasted some code without understanding what it is doing.
You are trying to push to an array, but you've declared your array as array, but trying to push to arr. Which is why it's not working.
var arr = new Array();
for(var i = 0; i <= 5; i++){
arr.push(i + 1);
}
console.log(arr);
alert(arr);
EDIT * You actually don't even need to declare the new Array(6), you can just use new Array() to push. However, if you would like to declare the size, you can do this instead.
var arr = new Array(6);
for(var i = 0; i <= 5; i++){
arr[i] = i + 1;
}
console.log(arr);
alert(arr);
You can also use this:
var arr = []; // create an empty object
for(var i = 0; i <= 5; i++){
arr.push(i + 1); // fill the object
}
console.log(arr);
alert(arr); // shows 1,2,3,4,5,6
This does what you are trying to achieve.
You can do it like
var a = new Array(6);
for(var i = 0;i < 6;i++){
a.push(i+1);
}
console.log(a);

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
}

Uncaught SyntaxError: Unexpected token for [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 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>"));
}

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