How to recover 2 results in the same function [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 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
}

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)

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

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

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();

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