convert array to separate object array - javascript

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

Related

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

array of array sum and breaks when if-condition meet

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

Join first two elements and create new array?

After playing around with the join() and slice() functions for hours, I have just found that you can't use either function on complex arrays. So I've come to get some help here.
I'm trying to get my data below:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
To look like this (output):
var result = [
["North: Tennis", 37, 25, 11, 9, 42, 13],
["East: Football", 41, 2, 3, 26, 47, 21],
["South: Rugby", 7, 22, 35, 45, 11, 46],
["West: Rugby", 30, 21, 44, 23, 4, 47],
["North East: Football", 35, 27, 12, 39, 34, 13],
["North West: Football", 23, 4, 41, 35, 9, 47]
];
Any help would be appreciated
As you want to turn one array into another array, it makes sense to .map the original array to another array by taking the first two elements of each subarray and concatenating them together, then building a new subarray by adding the rest of the elements (which we get with the rest parameter during the array destructuring) to a new array that contains the joined string.
let data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
let result = data.map(([s1, s2, ...rest]) => [`${s1}: ${s2}`, ...rest]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
References:
Array.prototype.map()
Array destructuring
Template literals
If I understand your question correctly, you are looking for the following output. I've used JavaScript's .map() function to restructure the data slightly. More information on .map here.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
const formattedData = data.map(row => {
return [
[row[0] + ': ' + row[1]],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7]
];
});
console.log(formattedData);
I'd write your code.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
// Final resulting array is created by concatenating the first and
// second index elements of each inner array of data. The rest data
// is being copied as it is using slice and spread operator.
const result = data.reduce(
(mem, cur) => [...mem, [`${cur[0]}: ${cur[1]}`, ...cur.slice(2)]],
[]
);
console.log(result);
You could use map method. It takes a callback which is executed for every element. It then returns a new array with the new data in each index. For example:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
newData = data.map((el, i, arr) => {
el[0] = `${el[0]}: ${el[1]}` // reassinging el[0]
el.splice(1, 1); // removing 2 array element
});

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