Steamroller, if typeof problems

Tell us what’s happening:

I created a function to be repeated that clears arrays by using join. It works fine if all arrays have the same number of inner arrays. For whatever reason, the if statement doesn’t seem to be working because it reports, typeError: x[i].join(’’); is not a function. I’m guessing it says that because individual strings cannot be joined. Which is why I included the (if) statement. Why isn’t that working?

Your code so far

function steamrollArray(arr) {
  // I'm a steamroller, baby
  function reReduce(x){
    for ( i = 0; i<x.length; i++ ){
      if ( typeof x[i][0] !== undefined ){//x[i][0] found here
        x[i] = x[i].join('');//runs regardless of x[i][0]
    return x;
  var newArr = reReduce(arr);
  return newArr;

steamrollArray([1, [2], [3, [[4]]]]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:

The typeof operator returns a string indicating the type of the unevaluated operand.

Here are a few examples:

typeof null // yields 'object'
typeof undefined // yields 'undefined'
typeof "text" //  yields 'string'
typeof 2 // yields 'number'
typeof NaN // yields 'number'
typeof [] // yields 'object'

Your if statement condition is comparing x[i][0] to the value undefined and the string “undefined”.

EDIT: Put the following console.log in right before the if statement to see what is happening:

console.log('x[' + i + '][0] = ' + x[i][0] + ' which has type of', typeof x[i][0])
1 Like