Reverse an array, but every element got undefined in font?

var arr_str = [ "i", "am", "cool", "i", "love" ];
Array.prototype.myReverse = function() {
  var result = [];
  var b = 0;
  for(var i = this.length-1; i>=0; i--) {
    result[b] += this[i];
  return result;

//results it’s [ “undefinedlove”, “undefinedi”, “undefinedcool”, “undefinedam”, “undefinedi” ]
emmm… why it’s like this? the “undefined” in front of every element it’s so funny…

result is an empty array, so result[b] is undefined. You may want to change the kind of operator you use there…

1 Like