array of array sum and breaks when if-condition meet - javascript

I have a js array like this.
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ]
var combination_before = [ 20,7,23, 19, 10, 15, 25, 8, 13 ];
These are the results of choosing seven out of nine.
I would like to return some elements only when sum of the elements is 100.
how do I make reducer if condition?

you can use filter for filtering your array and reduce for sum your nested arrays, and check in your filter which array sum is equal to 100
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
const result = test_arr.filter(arr => arr.reduce((a, b) => a + b, 0) == 100);
console.log(result);

const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
var resultArr = [];
for (var i = 0; i < test_arr.length; i++){
temp = test_arr[i].reduce(getSum)
if (temp == 100) {
resultArr.push(test_arr[i]);
}
}
function getSum(total, num) {
return total + num;
}
console.log(resultArr)

You could take a different approach at the moment, where you take all combination and return only the ones who match the conditions by length and sum.
function getCombinations(array, length, sum) {
function iter(array, temp) {
if (temp.length === length) {
if (temp.reduce((a, b) => a + b) === sum) {
result.push(temp);
}
return;
}
if (!array.length || array.length + temp.length < length) {
return;
}
iter(array.slice(1), temp.concat(array[0]));
iter(array.slice(1), temp);
}
var result = [];
iter(array, []);
return result;
}
var array = [20, 7, 23, 19, 10, 15, 25, 8, 13];
console.log(getCombinations(array, 7, 100));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related

Get all objects in one array from different arrays [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 days ago.
I have some data, but this data is a little bit strange. I have some arrays which have an array and then am getting an object. I need all these objects in one array. how can I do that?
[
[
{
travelerKey: 0,
travelPackageId: 7,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]
]
[
[
{
travelerKey: 0,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]
]
[
[
{
travelerKey: 0,
travelPackageId: 3,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]
]
must be [ {}, {}, {} ]
There are many ways to merge arrays in JavaScript
for exemple you can use javascript methode concat
const arrays = [
[
{
travelerKey: 0,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 1,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 80,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 2,
travelPackageId: 9,
travelerAge: 25,
minAge: 18,
maxAge: 74,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
],
[
{
travelerKey: 3,
travelPackageId: 9,
travelerAge: 25,
minAge: 11,
maxAge: 55,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 4,
travelPackageId: 9,
travelerAge: 25,
minAge: 15,
maxAge: 50,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]
]
let arrayResult = [];
arrays.forEach(array => {
arrayResult = arrayResult.concat(array);
})
console.log(arrayResult);
or you can use reduce method
let arrayResult = arrays.reduce(function(a, b){
return a.concat(b);
}, []);
console.log(arrayResult);
const arrays = [
[
{
travelerKey: 0,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 1,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 80,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 2,
travelPackageId: 9,
travelerAge: 25,
minAge: 18,
maxAge: 74,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
],
[
{
travelerKey: 3,
travelPackageId: 9,
travelerAge: 25,
minAge: 11,
maxAge: 55,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 4,
travelPackageId: 9,
travelerAge: 25,
minAge: 15,
maxAge: 50,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]
]
let arrayResult = arrays.reduce(function(a, b){
return a.concat(b);
}, []);
console.log(arrayResult);
the result:
[
{
travelerKey: 0,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 65,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 1,
travelPackageId: 9,
travelerAge: 25,
minAge: 20,
maxAge: 80,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 2,
travelPackageId: 9,
travelerAge: 25,
minAge: 18,
maxAge: 74,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 3,
travelPackageId: 9,
travelerAge: 25,
minAge: 11,
maxAge: 55,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
},
{
travelerKey: 4,
travelPackageId: 9,
travelerAge: 25,
minAge: 15,
maxAge: 50,
dayPremium: 45,
calculatedPremiumByInsurancesDays: 45
}
]

Error: Unsupported strips at Object.writeSTL in vtk.js

I am trying to create STL files using vtk.js of a vtkTubeFilter polyData.
The error am getting is:
Error: Unsupported strips
at Object.writeSTL
The code I'm using to get STL data:
const data = vtkSTLWriter.writeSTL(
polyData,
'ascii',
transform.getMatrix()
);
where, transform is a vtkTransform object and polyData is the polyData obtained from getOutputData() on vtkTubeFilter source.
As per vtk.js source code for writeSTL function, the number of strips must be 0 or null.
I checked on the polyData object, it has an array of length 140 for getStrips().getData().
let strips = newPolyData.getStrips().getData();
console.log(strips.length);
console.log(strips)
140
Uint32Array(140) [6, 1, 0, 21, 20, 41, 40, 6, 2, 1, 22, 21, 42, 41, 6, 3, 2, 23, 22, 43, 42, 6, 4, 3, 24, 23, 44, 43, 6, 5, 4, 25, 24, 45, 44, 6, 6, 5, 26, 25, 46, 45, 6, 7, 6, 27, 26, 47, 46, 6, 8, 7, 28, 27, 48, 47, 6, 9, 8, 29, 28, 49, 48, 6, 10, 9, 30, 29, 50, 49, 6, 11, 10, 31, 30, 51, 50, 6, 12, 11, 32, 31, 52, 51, 6, 13, 12, 33, 32, 53, 52, 6, 14, 13, 34, 33, 54, 53, 6, 15, …]
How can fix/debug this error, and still write it to STL file. Or, what am I doing wrong?

Compare each item with its next item and find each item big or small by comparing. And push the result to a new array for each

I have an array like the one below. How can I find every single value in the array is small or big compare to its next value. And I want to push the result in a new array like this same array. And if the items is the same then that's will be empty.
const array = [
[7, 10, 10, 10, 9, 13, 9, 9, 11, 12, 7, 8, 8, 8, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[6, 5, 6, 7, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 6],
[7, 7, 7, 8, 8, 8, 7, 7, 7, 5, 7, 7, 7, 7, 7],
[0, 63, 58, 65, 55, 68, 65, 65, 43, 58, 57, 55, 55, 70, 55],
[29, 26, 28, 27, 29, 29, 30, 33, 32, 33, 28, 28, 31, 31, 29],
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
];
This is my code but which is doesn't work.
const items = [];
function getOutRank() {
array.map((singleKey, index) => {
console.log("SINGLE KEY", singleKey);
for (let i = 0; i < singleKey.length; i++) {
if (singleKey[i + 1] > singleKey[i]) {
return items.push(<span key={index}>Down</span>);
}
if (singleKey[i + 1] === singleKey[i]) {
return items.push(<span key={index} />);
}
return items.push(<li key={index}>Up</li>);
}
});}
getOutRank();
For each singleKey, an array of all the compare results should be returned. Try like this:
const array = [
[7, 10, 10, 10, 9, 13, 9, 9, 11, 12, 7, 8, 8, 8, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[6, 5, 6, 7, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 6],
[7, 7, 7, 8, 8, 8, 7, 7, 7, 5, 7, 7, 7, 7, 7],
[0, 63, 58, 65, 55, 68, 65, 65, 43, 58, 57, 55, 55, 70, 55],
[29, 26, 28, 27, 29, 29, 30, 33, 32, 33, 28, 28, 31, 31, 29],
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
];
let items = [];
function getOutRank() {
items = array.map((singleKey, index) => {
var compare = [];
for (let i = 0; i < singleKey.length - 1; i++) {
if (singleKey[i + 1] > singleKey[i]) {
compare.push(`<span key=${index}>Down</span>`);
} else if (singleKey[i + 1] === singleKey[i]) {
compare.push(`<span key=${index}>Equal</span>`);
} else {
compare.push(`<li key=${index}>Up</li>`);
}
}
return compare;
});
}
getOutRank();
console.log(items);

convert array to separate object array

i have an array and i want convert this array to separate object , glad to help me, thanks
array = ["January", 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, "February", 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]
i want
result = [
{
month:'January',
days:[ 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]
},
{
month:'February',
days:[ 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]
}
]
First find out the indexes for strings in array, so that you can split array.
const process = (arr) => {
const result = [];
let last = -1;
for (let i = 0; i <= arr.length; i++) {
if (typeof arr[i] === "string" || !(i in arr)) {
if (last !== -1) {
result.push([last, i]);
}
last = i;
}
}
return result.map(([start, end]) => ({
month: arr[start],
days: arr.slice(start + 1, end),
}));
};
const array = ["January", 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, "February", 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]
console.log(process(array));

Replacing all elements in a nested Array

I have a challenge that I have been battling with for some time now. It's about replacing all the elements in an array that is in a grid form, but my solution is only replacing selected element of its choice not as I intended.
In this challenge, I want to replace the integer value that is divisible by two with the string "even" while the rest replaced with the string "odd".
/*
* - The numbers variable is an array of arrays.
* - a nested for loop to cycle through numbers.
* - it convert each even number to the string "even"
* - and Convert each odd number to the string "odd"
*/
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[column]%2===0){
myNumbers[column].splice(column,1,"even");
}else{
myNumbers[column].splice(column,1,"odd");
}
console.log(myNumbers[row][column]);
}
}
Code Output:


odd
12
23
12
45
45
78
66
223
3
34
odd
1
553
23
4
66
23
4
55
67
56
odd
553
44
55
5
428
452
3
12
31
55
odd
79
44
674
224
4
21
4
2
3
52
odd
51
44
1
67
5
5
65
4
5
5
odd
5
43
23
4424
74
532
6
7
35
17
odd
43
43
66
53
6
89
10
23
52
111
odd
109
80
67
6
53
537
2
168
16
2
odd
8
76
7
9
6
3
73
77
100
56
odd
You used the wrong variable for the row index
myNumbers[column]
//needs to be
myNumbers[row]
Also your if condition is using the wrong row index, and trying to compare against the whole array instead of the value in the array
if(myNumbers[column]%2===0)
//needs to be
if(myNumbers[row][column]%2===0)
Demo
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for (var row = 0; row < myNumbers.length; row++) {
for (var column = 0; column < myNumbers[row].length; column++) {
if (myNumbers[row][column] % 2 === 0) {
myNumbers[row].splice(column, 1, "even");
} else {
myNumbers[row].splice(column, 1, "odd");
}
}
}
console.log(myNumbers);
Probably easier to use a nested map instead:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = myNumbers.map(row => row.map(num =>
num % 2 === 0
? 'even'
: 'odd'
));
console.log(output);
Achieving the same thing with a for loop is much more verbose and confusing, and shouldn't be done in most cases (array methods have better abstraction and don't require manual iteration), but if necessary:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = [];
for (let rowIndex = 0; rowIndex < myNumbers.length; rowIndex++) {
const row = myNumbers[rowIndex];
const newRow = [];
for (let colIndex = 0; colIndex < row.length; colIndex++) {
const num = row[colIndex];
newRow.push(num % 2 === 0 ? 'even' : 'odd');
}
output.push(newRow);
}
console.log(output);
Why not simply set the value of myNumbers[row][column] instead of using splice?
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[row][column]%2===0) {
myNumbers[row][column] = "even";
} else{
myNumbers[row][column] = "odd";
}
console.log(myNumbers[row][column]);
}
}
In your inner loop you need to access the values using both indexes (i.e. myNumbers[row][column]). As it stands, you are only using the column index and therefore splicing values into the array containing the rows.

Categories

Resources