async eachseries how pass var after finish loop [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 7 years ago.
Improve this question
I want to pass var number to callback, after loop.
For example Invenotry array has 25 items.
So after this 25 steps I want to pass number into callback (inCb), because I need to check number value.
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if(sth == othersthm)
{
number++;
}
}, function(numb) {
check number value and do sth
});

You can just use number:
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if (sth == othersthm) {
number++;
}
}, function() {
//check number value and do sth
console.log(number);
});

Related

How do I get results? [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 1 year ago.
Improve this question
I am a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()

String as a Single value and Array as multiple value handling [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
If I select one value the I am getting value is like
val : "ABC"
But if I select multiple value my val is comming in array
val : ["ABC","DEF"].
Now because of this problem is neighther I can work on for single value nor multiple. Can anybody tell me how to handle this.
eg:
var myObj = {
data : "M1",
val : "Abc"
}
After adding another value it become
var myObj = {
data : "M2",
val : Abc,Def
}
function handleVal(val){
if ("string" === typeof val){
console.log("Do sth with string");
} else if (Array.isArray(val)){
console.log("Do sth with array")
}
}
handleVal("ABC");
handleVal([1,2])

Test the return value of my function correctly at the time of return [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 6 years ago.
Improve this question
I can not test the return value of my function correctly at the time of return.
My exercise is this: I must compare two arrays and return true if they have at least one identical element but I need some assistance in figuring out what is wrong with my code:
function duplicateElements(m, n){
function test (element){
return n.includes(element);
}
return m.filter(test) != [] ? true:false;
}
You have to test the length property of the return value.
function duplicateElements(m, n) {
function test(element) {
return n.includes(element);
}
return m.filter(test).length > 0 ? true : false;
}

How to declare a json object or array collection with variable size 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
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

How to improve selection sort? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is more of an academic/homework question?
Would it be better to change
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
to
$P.swap(arr, index_outer, index_min);
and always swap, as this is the special case when index_outer does have the smallest value? It would be a swap that does nothing, but at the same time it would not break anything. Because this does not happen often I suppose, it would reduce the amount of times an if check was used.
$P.swap = function (arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
$P.selectionSort = function (arr) {
var index_outer,
index_inner,
index_min,
length = arr.length;
for (index_outer = 0; index_outer < length; index_outer++) {
index_min = index_outer;
for (index_inner = index_outer + 1; index_inner < length; index_inner++) {
if (arr[index_inner] < arr[index_min]) {
index_min = index_inner;
}
}
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
}
return arr;
};
I don't think it would always be a good idea. What if array was partially/fully sorted, you would be wasting a call to $P.swap().
As for improving selection sort try sorting the array from both ends simultaneously by taking index_min and index_max. Though number of comparisons would remain same, number of passes would decrease hence decreasing total run time.

Categories