Use array length to push items in an array [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 2 years ago.
Improve this question
I have an items array with specific length depending on how many items in an order there are.
I have to calculate the packaging for it, for every 2 items I use the large box and the single item goes in a small box.
So if there are 5 items its 2 large boxes and 1 small box.
How do I do this using the array length? or any other way?

Use the modulo operator:
let numBigBoxes = 0;
let numSmallBoxes = 0;
if (items.length === 1) {
numSmallBoxes = 1;
} else if (items.length % 2 === 0) {
numBigBoxes = items.length / 2;
} else {
numBigBoxes = (items.length - 1) / 2;
numSmallBoxes = 1;
}

Related

How to nagivate an array based on another array in JS [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
Here are two arrays:
array1=['alex','joe','simon','sarah']
let index1= 0;
array2= [ [1,2,3,4,5,6,7],[1,2,3] , [1,2] ....]
let index2= 0;
Initially we are in array1 in index 0, I have a next button and whenever user clicks on it index2 moves and its value become 1, then with next click it is 2 and so on and so forth.
The problem is I want to change indeex1 whenever the navigation of the first array of array 2 is finished.
What I mean is when index2 is equal to 6 the index1 should change to 1.
How can I achieve this?
This is just an example
const array1 = ['alex','joe','simon','sarah']
let index1 = 0;
const array2 = [ [1,2,3],[1,2,3],[1,2]]
let index2= 0;
function userClick(arr){
if (arr[index1].length > index2) {
console.log(index1, index2);
index2++;
}
else {
index1++;
index2 = 0;
}
}
for (let i = 0; i < 7; i++) {
userClick(array2);
}

Why is my loop not working 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 2 years ago.
Improve this question
Why is my loop not working in JavaScript?
for (n = 1; n<4; n++)
{
var my_buttons = "";
my_buttons = my_buttons + `<div class="click" id="color${n}"></div>`;
document.getElementById("clicky").innerHTML = my_buttons;
}
You have two problems:
First you were overwriting my_buttons by declaring it within the loop. Second you were adding that overwritten variable to innerHTML within the loop also, so the content never got saved together.
my_buttons = "";
for (let n = 1; n<4; n++)
{
my_buttons = my_buttons + `<div class="click" id="color${n}"></div>`;
}
document.getElementById("clicky").innerHTML = my_buttons;

Increment all values of [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have that variable. var count_br = "4,7,10"
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
/// Increment here each values of count_br
// I want to get 8,11,14
//for the next loop I want to get 12,15,18
//etc
}
Each time if i > 0 I want to increment by 4 all the values of count_br.
How could i do to increment by 4 all the values .
thanks.
var count_br = "4,7,10";
var count_ = count_br.split(",");
var i;
for (i = 0; i < count_.length; ++i) {
for(let idx in count_) count_[idx] = (parseInt(count_[idx])+4).toString()
}
console.log(count_)
you should take the length of the variable... in this case 4... less 1 is 3 and use it on a for to go trougth all the positions of you array and for each iteration you add 4

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