2

How can I replace the numbers in the array which can be multiplied by 2 the string "even" and also for "odd".

var numbers = [
  [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 i = 0; i < numbers.length; i++) {
  for (var j = 0; j < numbers[i].length; j++) {
    if(numbers[i][j] % 2 === 0) {
      numbers[i][j] += " even";
    } else {
      numbers [i][j] += " odd";
    }
   
    console.log(numbers[j][i]);
  }
}

1
  • 2
    Your code works fine. The problem is the console.log(numbers[j][i]);. It should be console.log(numbers[i][j]);. (switch i and j). ` Commented Aug 8, 2017 at 8:07

2 Answers 2

2

If you mean replace the number by string , you can do this but the arrays must defined to be any :

 const numbers: any = [
  [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 (let i = 0; i < numbers.length; i++) {
  for (let j = 0; j < numbers[i].length; j++) {
    if ( numbers[i][j] % 2 === 0) {
      numbers [i][j] = 'even';
    } else {
      numbers [i][j] = 'odd';
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it is looking now like this: 243 odd 12 even 23 odd 12 even 45 odd 78 even 66 even But I would like to look like this : odd even odd....etc
I update the code and it works for me , please try it and give me a feedback
Yes it is working for me as well. I had to remove the + from += "even".
0

You need to switch the indices, for displaying the actual element. If not, you get some elements without changed values.

console.log(numbers[i][j]);
//                  ^  ^

var numbers = [
  [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 i = 0; i < numbers.length; i++) {
  for (var j = 0; j < numbers[i].length; j++) {
    if (numbers[i][j] % 2 === 0) {
      numbers[i][j] += " even";
    } else {
      numbers[i][j] += " odd";
    }
    console.log(numbers[i][j]);
  }
}

1 Comment

Thank you very much! I also had to remove the + from += so the result is only even, odd, even odd...without the numbers

Your Answer

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.