为什么我的代码会返回逗号?

时间:2017-05-13 23:28:44

标签: javascript arrays transpose

运行此代码后,我的返回有一堆“,”:

function transposeTwoStrings(arr) {
  var arr1 = arr[0];
  var arr2 = arr[1];
  var largerArray;

  if (arr1.length > arr2.length) {
    largerArray = arr1;
  } else if (arr1 < arr2) {
    largerArray = arr2;
  }

  var array = new Array(3);
  for (var i = 0; i < largerArray.length; i++) {
    array[i] = new Array(3);
  }
  for (var i = 0; i < largerArray.length; i++) {
    if (arr1[i] === undefined) {
      array += (" " + " " + arr2[i] + "\n");
    } else {
      array += (arr1[i] + " " + arr2[i] + "\n");
    }
  }
  return array;

}

var result = transposeTwoStrings(["Te", "xts"]);

console.log(result);

应该返回“T x \ ne t \ n s” 相反,我得到“,,,,,,,, T x e   小号 “

2 个答案:

答案 0 :(得分:0)

您需要解决许多问题才能获得输出 1.需要在调用站点的[]中传递参数,以便被调用的函数可以提取0和1索引的值 2.不要在array上使用+ =运算符,而是使用[]运算符 3.您将从函数返回的数组大小硬编码为3,不要这样做。

请参阅以下代码:

function transposeTwoStrings(arr) {
  var arr1 = arr[0];
  var arr2 = arr[1];
  var largerArray;

  if (arr1.length > arr2.length) {
    largerArray = arr1;
  } else if (arr1 < arr2) {
    largerArray = arr2;
  }

  //var array = new Array(3);
  var array = [];
  //for (var i = 0; i < largerArray.length; i++) {
  //    array[i] = new Array(3);
  //}
  for (var i = 0; i < largerArray.length; i++) {
    if (arr1[i] === undefined) {
      array[i] = (" " + " " + arr2[i] + "\n");
    } else {
      array[i] = (arr1[i] + " " + arr2[i] + "\n");
    }
  }
  return array;

}

var result = transposeTwoStrings(["Te", "xts"]);

console.log(result);

答案 1 :(得分:0)

我最终在其他人的帮助下开始工作;我最终得到了:

    function transposeTwoStrings(arr) {
var arr1 = arr[0];
var arr2 = arr[1];
var largerArray;

if (arr1.length > arr2.length) {
    largerArray = arr1;
} else if (arr1.length  < arr2.length) {
    largerArray = arr2;
} else if (arr1.length == arr2.length){
    largerArray = arr1;
}

var transposed = "";

for (var i = 0; i < largerArray.length; i++){
    //If arr1[i] is blank and [i] is not at the end of the array 
    if (arr1[i] === undefined && i != largerArray.length - 1){
        transposed += (" " + " " + arr2[i] + "\n");
    //If arr1[i] is blank and [i] IS at the end of the array    
    } else if (arr1[i] === undefined && i == largerArray.length - 1){
        transposed += (" " + " " + arr2[i]);
    //If arr2[i] is blank and [i] is NOT at the end of the array    
    } else if (arr2[i] === undefined && i != largerArray.length - 1){
        transposed += (arr1[i] + " " + " " + "\n");
    //If arr2[i] is blank and [i] IS at the end of the array    
    } else if (arr2[i] === undefined && i == largerArray.length - 1){
        transposed += (arr1[i] + " " + " ");
    //If [i] is at the end of the array    
    } else if (i == largerArray.length - 1){
        transposed += (arr1[i] + " " + arr2[i]);
    //If [i] is not at the end of the array    
    } else {
        transposed += (arr1[i] + " " + arr2[i])+ "\n";

    }
}
return transposed;

}

我知道这并不奇怪。

我知道,我并不接近成为程序员。

我感谢所有帮助过的人!