Why is my output a Matrix & method.push()?

Hello,

  1. When console.log(resultArray) the output is as show below, rather than just a one line array, Why is this?

“[
‘u’, ‘u’, ‘e’, ‘e’,
‘i’, ‘e’, ‘e’, ‘a’,
‘u’, ‘u’, ‘e’, ‘e’
]”

  1. Also, I cant quite understand why this code automatically know to push an additional letters e and u? Isn’t the " input[i] === ‘e’ || input[i] === ‘u’" a condition that has to be met? Why don’t I have to state what needs to be pushed in the method.push()?

" if (input[i] === ‘e’ || input[i] === ‘u’){
resultArray.push(input[i]);"

https://www.codecademy.com/courses/introduction-to-javascript/projects/whale-talk

let input = ‘turpentine and turtles’;
const vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
let resultArray = ;

for (let i = 0; i < input.length; i++) {
console.log(i)
if (input[i] === ‘e’ || input[i] === ‘u’){
resultArray.push(input[i]);
}
for (let j = 0; j < vowels.length; j++) {
console.log(j)
if (input[i] === vowels[j]) {
console.log(input[i])
resultArray.push(input[i])
}
}
}

let resultString = resultArray.join().toUpperCase()
console.log(resultString)
console.log(resultArray)

What needs to be pushed is already in input[i] (which has a value of 'e' or 'u' inside the if-block).

If you didn’t use input[i] there, then you could do something like:

  if (input[i] === 'e') {
    resultArray.push('e');
  }
  else if (input[i] === 'u') {
    resultArray.push('u');
  }
1 Like

The first one is just a result of the internal settings for when the console.log() function should start wrapping its output into the next line. It doesn’t mean that your output is being represented as a matrix. Anyways, a matrix would look more like an array of arrays:

const matrixArray = [
  [1, 1, 1, 1],
  [2, 2, 2, 2],
  [3, 3, 3, 3]
]
1 Like