0

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

Attersson
4,8841 gold badge19 silver badges32 bronze badges
asked Jun 2, 2018 at 22:02
0

4 Answers 4

2

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

answered Jun 2, 2018 at 22:12
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with you completely. Thanks
2

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

answered Jun 2, 2018 at 22:04

4 Comments

i'm confused to why this is working, shouldn't there be a return statement ?
Wow, but this seems too advanced to grasp. Please come down to my level. Can you break it down first using my beloved nested for, thereafter this advanced method...
@ZohirSalakCeNa Arrow functions without { blocks have implicit return.
Oh i see now, i still only use function(){}, Thanks :)
1

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]);
 }
 }
answered Jun 2, 2018 at 22:12

2 Comments

Wow, I'd never thought of it this way. I am going right away to implement this.
This is the most straight forward way to perform the replace or by using the splice method noted by @Patrick Evans. Thanks xrisk
0

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.

answered Jun 2, 2018 at 22:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.