Reputation: 874
What is the method to evenly split an array into two. Then, position the second half underneath the first all for the purpose of comparing whether or not a new final array should receive a zero or one in the slots.
var master_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38];
var first_half = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
var second_half = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38];
var final_usable_array_reduced = [];
So, given a form with 38 check boxes, if the 5th, 19th, 37th, and 38th check boxes were checked, the final_usable_array_reduced
would be
final_usable_array_reduced = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1];
final_usable_array_reduced[4] = **represents the 5th <strong>or</strong> 24th box being checked**
final_usable_array_reduced[18] = **represents the 19th <strong>or</strong> 38th boxes being checked**
Upvotes: 0
Views: 547
Reputation: 30893
Consider the following.
$(function() {
function addChecks(count, tObj, cont) {
var i;
if (cont == undefined) {
i = 0;
} else {
i = parseInt($("input:last").val()) - 1;
count = i + count;
}
for (i; i <= count; i++) {
$("<input>", {
type: "checkbox",
value: (i + 1),
title: (i + 1)
}).appendTo(tObj);
}
}
function checksToArray() {
var arr = [];
$("input[type='checkbox']").each(function() {
arr.push($(this).prop("checked"));
});
return arr;
}
function compHalfArr(a) {
var arr = [];
var h = Math.floor(a.length / 2);
for (var i = 0; i <= h; i++) {
arr.push(a[i] || a[(i + h)]);
}
return arr;
}
addChecks(14, $(".section-1"));
addChecks(14, $(".section-2"), true);
$("button").click(function() {
var allChecks = checksToArray();
var compChecks = compHalfArr(allChecks);
$(".results").html("<span>" + compChecks.join("</span><span>") + "</span>").find("span:last").remove();
});
});
.checks input {
margin-right: 20px;
}
.results span {
display: inline-block;
font-family: Arial, sans-serif;
width: 40px;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checks section-1"></div>
<div class="checks section-2"></div>
<button>Check</button>
<div class="results"></div>
You can do a loop using a halfway marker.
Upvotes: 1
Reputation: 178
Check this one:
var array = [0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0];
var firstHalf = array.slice(0,array.length/2);
var secondHalf = array.slice(array.length/2);
var result = firstHalf.map(function(e,i){
return e || secondHalf[i];
});
console.log(result);
What I'm doing here is spliting the array into two halfs with slice
(considering that the array elements are even, of course), and after that I map the first one returning 1 (or true, is the same) if firstArray[i]
or secondArray[1]
is 1.
Upvotes: 1
Reputation: 147166
I think you can implement this with a simple for
loop over half the master_array
, or
ing the values in the bottom half of the array with those in the top half:
var master_array = new Array(38).fill(0);
master_array[4] = 1;
master_array[18] = 1;
master_array[36] = 1;
master_array[37] = 1;
let len = master_array.length / 2;
final_usable_array_reduced = new Array(len);
for (let i = 0; i < len; i++) {
final_usable_array_reduced[i] = master_array[i] | master_array[i + len];
}
console.log(final_usable_array_reduced);
Upvotes: 1